📅 2016-Mar-16 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ hard link, inode, ls ⬩ 📚 Archive
A hard link is a reference that links a file or directory name to an inode in the filesystem. Hard links are created by default when a file or directory is created. Hard links can also be created in Linux (or any Unix) using the ln
command.
The inode number of any file or directory can be viewed using ls -i
:
$ mkdir foo
$ ls -id foo
1842306 foo/
Here, 1842306
is the inode number of the foo
directory we created.
The number of hard links can be viewed in the second column of ls -l
:
$ ls -ld foo
drwxrwxr-x 2 joe joe 4096 Mar 16 21:59 foo/
We can see that an empty directory starts off with 2 hard links. These are from foo
and foo/..
respectively.
In contrast, a file starts off with a single hard link.
The number of hard links associated with the inode of a directory is usually the number of its subdirectories, plus two. This is because the ..
entry inside each subdirectory points back to the inode of its parent directory.
Here is an illustration:
$ mkdir -p foo/bar
$ mkdir -p foo/buzz
$ mkdir -p foo/pizza
$ ls -ld foo/{,.,*/..}
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/.
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/bar/..
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/buzz/..
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/pizza/..