I can print the number of hardlinks to files in directory by using find . type f -printf %f %n\n  but this will also print the hardlink not only original files: Example:  file1 is an original file.  hardlink1 is a hardlink to the original file1.Using this code It will print following :file1 2hardlink1 2I want this problem to be solved only by using find. I want to print only the original file.
Printing the file with number of hardlinks
bash;shell;shell script;find
You have misunderstood how hard links work. There is no original. All files are simply hardlinks to an inode. Therefore, hardlinks don't actually link to files, they link to inodes. To illustrate, consider this file:$ touch file$ ls -li file3282140 -rw-r--r-- 1 terdon terdon 0 May  3 16:27 fileAs you can see above, file points to the inode 3282140. Now, if we create a softlink to it:$ ln -s file softlink$ ls -li 3282140 -rw-r--r-- 1 terdon terdon 0 May  3 16:27 file3282141 lrwxrwxrwx 1 terdon terdon 4 May  3 16:29 softlink -> fileThe softlink is pointing to file but the link itself (which is also a filesystem object, another file) has a different inode (3282141). However, if we create a hardlink, that will share the same inode:$ ln file hardlink$ ls -litotal 03282140 -rw-r--r-- 2 terdon terdon 0 May  3 16:27 file3282140 -rw-r--r-- 2 terdon terdon 0 May  3 16:27 hardlink3282141 lrwxrwxrwx 1 terdon terdon 4 May  3 16:29 softlink -> fileSo, if I now add some text to hardlink, it is exactly the same as though I added text to file itself:$ echo foo > hardlink $ cat hardlink foo$ cat filefooAnd, since softlink is pointing to file, I will see the contents of file if I cat it:$ cat softlink fooThe same happens if I add text to file, hardlink and file are equivalent:$ echo bar >> file$ cat hardlink foobarNow, if I delete file, softlink will be a broken link but hardlink won't be affected:$ rm file $ ls -litotal 43282140 -rw-r--r-- 1 terdon terdon 8 May  3 16:33 hardlink3282141 lrwxrwxrwx 1 terdon terdon 4 May  3 16:29 softlink -> file$ cat softlink cat: softlink: No such file or directorySince the link's target has been removed, the softlink is broken. The hardlink, however, is still there since it was pointing to the same inode:$ cat hardlink foobarAll this is to say that there is simply no way to differentiate a hardlink from the file it was created to point to because, unlike softlinks, it wasn't actually created pointing to a file. All files are hardlinks pointing to an inode of the file system. In fact, deleting a file simply removes the link to the inode. It doesn't actually remove the data. The system keeps a count of the links pointing to each inode and simply considers inodes available for writing if the link count is 0. What you can do, is find all links pointing to an inode and delete all but one. If we go back to where we were at the beginning:$ rm *$ touch file$ ln file hardlink$ ls -l file hardlink-rw-r--r-- 2 terdon terdon 0 May  3 16:39 file-rw-r--r-- 2 terdon terdon 0 May  3 16:39 hardlink           ^           |---- the number of linksThe output of ls -l includes the number of links pointing to an inode. Therefore, you can iterate over its output (this is one of the few cases where parsing ls is safe) and delete any files with a link number >1:for file in *; do [ $(ls -l $file | cut -d' ' -f2) -gt 1 ] && rm $file; doneThat will delete all hardlinks but one, as long as all the hardlinks are in the same directory.Further reading: http://teaching.idallen.com/dat2330/04f/notes/links_and_inodes.html