Hard links in Linux are directory entries that refer to the same inode number within the same filesystem. What is the minimum link count for a directory name on a Linux system?
- 4 The directory, a hard link to itself, a hard link to its parent, and hard link from its parent.
- 3 The directory name itself, a hard link to itself named " ." and the hard link ".." to its parent.
- 0 Because users cannot hard link additional names to a directory name.
- 2 The directory name itself, and a hard link to itself (named "."), within the directory.
EXPLANATION
Users cannot hard link directories:$ mkdir This_dir $ ln This_dir That_dir ln: ‘This_dir’: hard link not allowed for directoryBut the system makes two hard links within a directory when it is created. One of these is named "." and it is a link to the newly created directory itself, so the new directory will have a link count of 2, which is the minimum link count for a directory.
The other is named ".." and it is a link to the parent directory, and will add to the parent's link count.
$ ls -ild Link.test ## list "i"node, "l"ong list, for the "d"irectory itself. ls: cannot access Link.test: No such file or directory $ mkdir Link.test ## make the directory. $ ls -ild Link.test 2238981 drwxr-xr-x 2 dan users 4096 Dec 26 13:53 Link.test $ #Link count......^ The newly created directory has 2 hard links. $ ls -ild "Link.test" "Link.test/." #Quoting not required, but more readable 2238981 drwxr-xr-x 2 dan users 4096 Dec 26 13:53 Link.test 2238981 drwxr-xr-x 2 dan users 4096 Dec 26 13:53 Link.test/.When making a new directory, the system will also create a link to the parent directory named ".."
So every time a directory is created, the link count of the parent directory increases by 1.
$ pwd /home/dan/Test.link.dir $ ## List inode numbers in a long listing, directory only, for "Link.test" and "Link.test/." $ ls -ild "Link.test" "Link.test/." 2238981 drwxr-xr-x 2 dan users 4096 Dec 26 13:53 Link.test 2238981 drwxr-xr-x 2 dan users 4096 Dec 26 13:53 Link.test/. $ #................^ Link count is 2 for each entry and inode numbers are identical, 2238981. $ mkdir Link.test/subdir ## make a subdirectory under "Link.test". $ ## Now check the link count, including the newly created subdirectory's parent, " .. " $ ls -ild "Link.test/" "Link.test/." "Link.test/subdir/.." 2238981 drwxr-xr-x 3 dan users 4096 Dec 26 15:55 Link.test/ 2238981 drwxr-xr-x 3 dan users 4096 Dec 26 15:55 Link.test/. 2238981 drwxr-xr-x 3 dan users 4096 Dec 26 15:55 Link.test/subdir/.. $ #................^ Link count has increased from 2 to 3 after creating "subdir". $ #+ The 3 names above, "Link.test", "Link.test/." and "Link.test/subdir/.." reference $ #+ the same inode, 2238981.c.f. https://www.tldp.org/LDP/gs/node5.html
"... a directory is actually just a file containing information about link-to-inode associations. Also, every directory contains at least two hard links: ``.'' (a link pointing to itself), and ``..'' (a link pointing to the parent directory)."
The root directory of a filesystem does not have a parent directory, so "/" and "/." and "/.." all reference the same inode. That is, the parent directory of root, "/..", is "/"
E.g.
$ ls -ild "/" "/." "/.." 2 drwxr-xr-x 32 root root 4096 Oct 26 13:54 / 2 drwxr-xr-x 32 root root 4096 Oct 26 13:54 /. 2 drwxr-xr-x 32 root root 4096 Oct 26 13:54 /.. $ #..........^^ Note that this link count also includes parent references (..) from subdirectories.
See also: http://teaching.idallen.com/cst8207/13w/notes/notes/450_file_system.html
0 comments:
Post a Comment