0

Take /etc dir as example. On my system it has 144 links. And as dir's can't have hardlinks (this is my understanding), the number 144 should refer to softlinks (1 original + 143 softlinks).

$ ll -i
total 84
      2 drwxr-xr-x  20 root root  4096 Aug 18 01:21 ./
      2 drwxr-xr-x  20 root root  4096 Aug 18 01:21 ../
     12 lrwxrwxrwx   1 root root     7 Aug 18 01:19 bin -> usr/bin/
1048577 drwxr-xr-x   4 root root  4096 Sep  1 15:53 boot/
5636097 drwxr-xr-x   2 root root  4096 Aug 18 01:21 cdrom/
      2 drwxr-xr-x  23 root root  4920 Sep  3 11:20 dev/
 262145 drwxr-xr-x 144 root root 12288 Sep  3 09:34 etc/

I am trying to trace the softlinks (out of curiosity and to learn in advance for any future uses) but having no luck.
I tried -

$ sudo find  /  -not -path "/mnt/*" -samefile /etc  
/etc
find: ‘/run/user/1000/doc’: Permission denied 
...
samshers
  • 678

1 Answers1

2

Directories do have hard links, but they’re not arbitrary. Each directory contains a hard link to its parent directory, ..; your /etc contains 142 sub-directories. The other two are /etc itself and /etc/..

To find symlinks, you need to tell find to follow them:

find -L / -xdev -samefile /etc

Instead of excluding paths you’re not interested in, it’s more effective to list all the mount points you are interested in and tell find not to descend into other file systems. This avoids processing /proc, /sys etc.

Stephen Kitt
  • 434,908
  • Since you're already using GNU find, better use -lname to find "softlinks" to some file or directory, rather than that tricky and limiting -L -samefile contraption. –  Sep 03 '20 at 08:31
  • @user414777 -lname matches link names exactly, so it misses anything that skips around using .. or uses a link containing directories. – Stephen Kitt Sep 03 '20 at 08:52
  • Indeed, but that sounds more like a feature than a bug. Trying to find an undeterminate subset of all the possible paths through which you can access a file doesn't feel like a well thought-out course of action ;-) –  Sep 03 '20 at 09:04
  • The point isn’t to find all the possible paths (there are an infinity of those, which I guess is what you’re referring to), it’s to find all the existing symlinks to a given directory or file, and that’s a determinate set, which -lname can’t find. Could you explain why -L -samefile is limiting? – Stephen Kitt Sep 03 '20 at 09:08
  • I don't think there are an infinity of paths to a file, unless symlink loops and unlimited path lengths are accepted, which they aren't. Your example may find the same symlink multiple times, and will also find hardlinks, without any obvious way to refine the result. Also, this answer would be improved if it was mentioned that only in some filesystems subdirs increase the link count of the parent dir. –  Sep 03 '20 at 17:52