1

Here is how I find hard links located in /tmp:

$ for file in /tmp/*; do  if [ "$(stat -c %h -- "$file")" -gt 1 ]; then  ls "$file" -l ; fi; done
total 0
srwxrwxr-x 1 t t 0 Mar 20 23:01 debconf.socket
total 0
srwxrwxr-x 1 t t 0 Mar 20 22:58 debconf.socket
total 0
srwxrwxr-x 1 t t 0 Mar 20 23:00 debconf.socket
total 0
-rw-rw-r-- 2 t t 39675802 Mar 23 01:06 /tmp/dd
total 0
lrwxrwxrwx 1 t t 5 Mar 21 12:52 d1 -> ../d1
total 676
-rw------- 1 t t 688899 Mar 22 21:46 image.png
total 0
total 352
-rw-r--r-- 1 t t 345008 Jun  2  2008 iwlwifi-5000-1.ucode
-rw-r--r-- 1 t t   2114 Jun  4  2008 LICENSE.iwlwifi-5000-ucode
-rw-r--r-- 1 t t   5099 Jun  4  2008 README.iwlwifi-5000-ucode
total 360
-rw-r--r-- 1 t t 353240 Apr 23  2009 iwlwifi-5000-2.ucode
-rw-r--r-- 1 t t   2114 May 19  2009 LICENSE.iwlwifi-5000-ucode
-rw-r--r-- 1 t t   5097 May 19  2009 README.iwlwifi-5000-ucode
total 348
-rw-r--r-- 1 t t 340688 Apr 20  2011 iwlwifi-5000-5.ucode
-rw-r--r-- 1 t t   2046 Dec  2  2010 LICENSE.iwlwifi-5000-ucode
-rw-r--r-- 1 t t   4922 Dec  2  2010 README.iwlwifi-5000-ucode
total 4
-rw-rw-r-- 1 t t 50 Mar 22 23:23 xauth-1000-_0
total 0
srw------- 1 t t 0 Mar 22 23:23 kdeinit4__0
srwxrwxr-x 1 t t 0 Mar 22 23:23 klauncherhX1003.slave-socket
total 0
total 4
-rw-rw-r-- 1 t t 402 Mar 13 07:39 note~
total 8596
-rw-r--r-- 1 t t     283 Feb  8  2106 content.xml

Most of detected files are not hardlinks, as there is only 1 reference to them. I wonder why I get these false positives? How can I find the hard links?

1 Answer 1

3

You are also including directories in your loop. Exclude them:

for file in /tmp/*; do  if [ -f "$file" ] && [ "$(stat -c %h -- "$file")" -gt 1 ] ...

Directories can have hard link counts higher than 1 depending on the filesystem. Why does a new directory have a hard link count of 2 before anything is added to it?

You could also use ls -ld "$file", and have the directories listed, not their contents.

9
  • +1 for ls -ld "$file", both for adding the d option (which, IMHO, you should always use unless you specifically want to list a directory) and also for the order. I am astonished that ls "$file" -l works, and I strongly suspect that it's not as portable as the traditional "[OPTION]... [FILE]..." order. Mar 23, 2015 at 19:32
  • @Scott: how bad are ls . -l and rm mydir -r?
    – Tim
    Mar 24, 2015 at 17:07
  • @Tim on Linux, which has GNU utilities, not so bad. Most GNU utilities support arbitrary arrangement of options and arguments.
    – muru
    Mar 24, 2015 at 17:08
  • OK, my problem is that it’s not POSIX compatible. See getopt(3): “If [POSIXLY_CORRECT] is set, then option processing stops as soon as a nonoption argument is encountered.” This copy says the same thing. So POSIXLY_CORRECT=1 ls . -l will say ls: cannot access -l: No such file or directory and then list the current directory (in short form). I remember, before POSIX, that this is the way it was, always, period. … (Cont’d) Mar 24, 2015 at 19:34
  • (Cont’d) . … So, if you get accustomed to typing things like ls . -l, you will have to re-learn how to type simple commands if you ever land on a POSIX-compatible system. … … … … And, yes, rm seems to be a problem area. Historically, rm foo -r and rm ./-r were the two ways to delete a file called -r — this is before -- was defined. … (Cont’d) Mar 24, 2015 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .