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?

Tim
  • 101,790

1 Answers1

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.

muru
  • 72,889