Search code examples
filesystemsposixsymlinkinodehardlink

Does creating a hard link create a new inode?


From this StackOverflow thread, I get to know that symbolic links have their own inodes that are different from those of their targets. This can be verified with ls -i. The command will show that a symbolic link and its target have distinct inode numbers. But how things are with hard links? Does creating a hard link create a new inode, or it simply adds an entry in the data of the containing directory without creating any new inode? In other words, does creating a hard link create a real file? Creating a symbolic link certainly does.


Solution

  • The same method used to show ln -s creates a new inode can be used to verify ln alone does not. Creating a hard link creates a directory entry pointing to the very same inode (here number 26477281).

    $ touch foo 
    $ ls -li foo
    26477281 -rw-r--r-- 1 jlliagre jlliagre 0 Nov 10 21:39 foo
    $ ln foo bar
    $ ls -li foo bar
    26477281 -rw-r--r-- 2 jlliagre jlliagre 0 Nov 10 21:39 bar
    26477281 -rw-r--r-- 2 jlliagre jlliagre 0 Nov 10 21:39 foo
    

    Note that the link count was changed from 1 to 2 after the hard link creation.