1

Both programs stat and ls -l tell about the number of (hard) links to file(s).

Going under the assumption that one usually cannot create hardlinks for directories in Linux it confuses me to see outputs like this

$ ls / -l | grep etc
drwxr-xr-x 163 root     root       12288 Oct 11 17:32 etc

$ stat /etc | grep Links
Device: fc00h/64512d    Inode: 655361      Links: 163

Are there indeed 163 links to etc? I am confused and I would like to know what the number of links means with respect to directories?

I have tested that creating symbolic links does not have an influence to the number (which seems logic as the system would have hell of a job to keep track of all symbolic links)

Also I have tested that for regular files the number really reflects the hardlinks.

  • Remember that . and .. are hardlinks. In your case /etc has 161 subdirectories, all of whom contain a .. hardlink to it. See here and here. – jw013 Oct 11 '14 at 19:13
  • @jw013 thanks for that bright idea! I was really confused! But indeed the only ever kind-off hardlinks to directories are the ... Thanks for clearing my confusion. This also gives the reason why the linkcount always ever starts with 2 for newly made directories. – humanityANDpeace Oct 11 '14 at 19:17
  • you can test such as find /etc/ -type l -iname "*" |wc -l – PersianGulf Oct 11 '14 at 19:32
  • @jw013 Indeed the question "How to find all the links to a directory" seems to eventually lead to much the same information. The value of this question (which indeed could be answered much in the same way and I might give this answer myself) would be that it does put the focus on the fact where the links to the directory result from in the question. The other question is already aware of that. A confused information seeker is more likely to get the info by this question than with the other. Yet if you think different I can delete the question. – humanityANDpeace Oct 12 '14 at 07:55

0 Answers0