ハードリンクの検索が失敗するのはなぜですか?

ハードリンクの検索が失敗するのはなぜですか?

次の場所にあるハードリンクを見つける方法は次のとおりです/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

検出されたファイルのほとんどはそのファイルへの参照のみであるため、ハードリンクではありません1。なぜこのような誤検出が起こるのだろうか。ハードリンクを見つける方法は?

ベストアンサー1

また、ループにディレクトリを含めます。除外:

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

ファイルシステムによっては、ディレクトリ内のハードリンクの数が 1 を超えることがあります。何か追加する前に、新しいディレクトリのハードリンク数が2であるのはなぜですか?

ls -ld "$file"を使用してコンテンツの代わりにディレクトリを一覧表示することもできます。

おすすめ記事