0

When I create directory, and type ls -l. It will show 2 links while I create file and check the long listing via ls -l command then it only show 1 link. Can anyone tell me the reason behind it.... Long listing of Home directory

Over here you can see file(e.txt) has 1 link while directory (amit) has 2 link....

2 Answers2

0

I always understood the extra link to be due to the "." entry that is created automatically when a directory is created. That is effectively a hard link to the directory.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I'm not sure, but I think this is a homework question in Maurice Bach's book. In older versions of Unix, there was no mkdir(2) system call. You had to mknod() (one link) and then make 2 additional links: one from "." to the new node (the second link), and then link ".." to the parent node (changing the parent's link count). Hence, 2 links per initial directory. I can't be sure about the exact book ("The Design of the UNIX Operating System"?), but that's why directories on Unix-like file systems have at least 2 links. It's also why they added the mkdir() system call; the earlier 3-step process was tedious and error prone.

Erik Bennett
  • 1,049
  • 6
  • 15
  • I'm not sure I understand. A file's name is 1 link. An inode with no links to it is an unreferenced inode, and should be on the free list. If it isn't, the file system needs an `fsck(8)`. – Erik Bennett May 07 '17 at 06:51
  • If you haven't read them, I suggest either the Maurice Bach book (above), or Leffler and Kirk McKusick's (et al) "The Design and Implementation of the 4.3 Bsd Unix Operating System". If someone knows of a descent book on Linux, please let us know. – Erik Bennett May 07 '17 at 07:00
  • Oops. I meant "decent" in my earlier post. I chose those two books, even though they're old because they are textbooks made for teaching. They contain a good deal of history and rationale about the design choices made in the systems they describe. – Erik Bennett May 07 '17 at 17:57