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
  • 98,580
  • 191
  • 570
  • 977

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
  • 69,900
  • 13
  • 192
  • 292
  • +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. – Scott - Слава Україні Mar 23 '15 at 19:32
  • @Scott: how bad are `ls . -l` and `rm mydir -r`? – Tim Mar 24 '15 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 '15 at 17:08
  • OK, my problem is that it’s not POSIX compatible. See [getopt(3)](http://man7.org/linux/man-pages/man3/getopt.3.html): “If [POSIXLY_CORRECT] is set, then option processing stops as soon as a nonoption argument is encountered.” [This copy](http://linux.die.net/man/3/getopt) 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) – Scott - Слава Україні Mar 24 '15 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) – Scott - Слава Україні Mar 24 '15 at 19:35
  • (Cont’d) . … I’m concerned that somebody who remembers the olden days might accidentally type `rm foo -r` out of habit. But I’m struggling to see how this would actually be a problem, though. I guess the biggest risk is if the user says `rm foo.d -r`, where `foo.d` is a directory (especially if it has stuff in it). The user might think, “This will skip over `foo.d`, because `rm` doesn’t remove directories, and then it will remove `-r`.” Of course, it’s pretty unwise to say `rm foo.d …` if `foo.d` exists and you don’t want it deleted. – Scott - Слава Україні Mar 24 '15 at 19:37
  • P.S. I checked our canonical question, [How do I delete a file whose name begins with “-” (hyphen, a.k.a. dash or minus)?](http://unix.stackexchange.com/q/1519/23408), and (fortunately) it does ***not*** offer `rm foo -r` as an answer. – Scott - Слава Україні Mar 24 '15 at 19:41
  • Another (somewhat more authoritative?) source document: [The Open Group Base Specifications Issue 7, Chapter 12. Utility Conventions](http://pubs.opengroup.org/stage7tc1/basedefs/V1_chap12.html) says 'The arguments following the last options and option-arguments are named "operands".' and '**Guideline 9:** All options should precede operands on the command line.' – Scott - Слава Україні Apr 01 '15 at 13:55
  • Just to throw another log on the fire: [this](http://unix.stackexchange.com/q/203264/23408) is, arguably, an example of what people do when they believe that words on a command line can be arbitrarily rearranged. – Scott - Слава Україні May 14 '15 at 09:10