Wednesday, October 31, 2012

Link count: File vs Directory



  One of the results of the ls -l command  is the link count. What is this link count? How it is useful? What is the difference between the link count of a file and a directory? Let us discuss in this article.

1. What is the link count of a file?
      The link count of a file tells the total number of links a file has which is nothing but the number of hard-links a file has. This count, however, does not include the soft-link count.
Note: The soft-link is not part of the link count since the soft-link's inode number is different from the original file.

2. How to find the link count of a file or directory?
   The link count can be seen by using the ls -l command :
$ ls -l test.c
-rwxr-xr-x 1 guru users 267 Jul 18 16:59 test.c
    By default, a file will have a link count of 1 i.e., any new file created will have a link count 1.

Let us now create a soft link and a hard link to the file test.c:
$ ln test.c test1.c     #hard link
$ ln -s test.c test2.c  #soft link
$ ls -l test*.c
-rwxr-xr-x 2 guru users 267 Jul 18 16:59 test1.c*
lrwxrwxrwx 1 guru users   6 Jul 31 16:13 test2.c -> test.c*
-rwxr-xr-x 2 guru users 267 Jul 18 16:59 test.c*
   The file, test.c, now has a link count of 2 because there is a hard-link created for the file.   Also note that the creation of soft-linked file did not have any impact on the link count.

3. Does the link count decrease whenever the hard-link is deleted?
     Yes. When the hard link file is moved or deleted, the link count of the original file gets reduced.
$ rm test1.c
$ ls -l test*.c
-rwxr-xr-x 1 guru users 267 Jul 18 16:59 test1.c*
lrwxrwxrwx 1 guru users   6 Jul 31 16:13 test2.c -> test.c*
4. When does the link count of a directory change?
       The link count of a directory increases whenever a sub-directory is created.
 Let us create a directory "xyz" :
$ mkdir xyz
$ ls -ld xyz
drwxr-xr-x 2 guru users 4096 Jul 31 16:31 xyz/
    A directory "xyz" is created and the default link count of any directory is 2. The extra count is because for every directory created, a link gets created in the parent directory to point to this new directory.

  Let us create 2 sub-directories under the "xyz" directory"
$ mkdir -p xyz/abc
$ mkdir -p xyz/efg
$ ls -ld xyz
drwxr-xr-x 4 guru users 4096 Jul 31 16:32 xyz/
  Check out the link count now!!. The count increased because for every directory created, a link is created for the parent directory to access it. Since 2 new directories are created,  the link count increased by 2 and hence became 4.  Hence, we can also say that the link count of a directory minus 2 gives you the total number of sub-directories present in the directory.
   Like the link count of the file, the link count of the directory also reduces by 1 whenever a sub-directory is moved or deleted.

3 comments:

  1. What is the use of inodes,soft links and hard links.And also would you please let me know -Practically in which scenario i need to go for creating this stuff.

    ReplyDelete
    Replies
    1. we have quite a few articles on inodes and soft/hard links..just search for "inodes" in the search box at the right hand side, you will get it...

      Delete