Hard link vs Symbolic link: what’s the difference.
An inode is a data structure on a filesystem on Linux and other Unix-like operating systems that stores all the information about a file except its name and its actual data.
A filesystem itself is the hierarchy of directories (the directory tree) that is used to organize files on a computer. On Unix-like operating systems, the directories start with the root directory (forward slash /
), which contains a series of subdirectories, which may contain further subdirectories, etcetera.
A file is a named collection of related information that appears to the user as a single entity. It doesn’t contain information about itself (something like its size, when it was created or where it is located on the system). Such information about a file is its metadata.
Storage refers to computer devices that can hold data for relatively long periods of time (SSD or HDD). A directory in Unix-like operating systems is a special type of file that associates filenames with a collection of inodes.
When a file is created, it is assigned both a name and an inode number — an integer that is unique for the filesystem. Both the file names and their corresponding inode numbers are stored as entries in the directory that appears to the user to contain the files. That means that directory associates filenames with inodes.
Whenever user or a program refers to file by it’s name, the operating system uses that name to look up for the inode, which then gives the information to the system to perform further operations. That means that filename in a Unix-like operating system is an entry in a table with inode numbers, and it is not associated directly with a file (in contrast to MS Windows, for example). This detaching of a file’s name from its other metadata is what allows the system to implement hard links and have multiple names for any file.
A hard link is an entry in a directory that contains a pointer to the inode with file’s metadata.
When a new hard link to a file is created, both links share the same inode number because the link is not a copy of the file.
A file’s inode number can easily be found by using the ls
command with its -i
option. For example, the following will show the name of each object in the current directory together with its inode number:
ls -i
Lets illustrate this by createing a file and checking its inode number:
Then we will create the hard link to it:
ln cat mouse # format is `sourcefile target`
We can check the result by listing the current directory and filtering the result by the inode number. As you can see both cat
and mouse
are pointing to the same inode.
Once a hard link has been made, it points to the inode. In our case the mouse
hard link points to the same inode as the cat
. Deleting, renaming or moving the original file (in our case cat
) will not affect the hard link as it links to the underlying inode.
As you can see above, we removed original file, named cat
and we still have the mouse
file, which points to the same inode (number 4303388407
). What is important to keep in mind, is that any changes to the data on the inode are reflected in all files that refer to that inode. Let me demonstrate that by creating the hard link with the name “cat”, that will point to the same inode as mouse
. Let’s count how many words in both files:
As we can see, both have 1 word (text after number is not a word, but the name of the file). Now let’s append another word to our newly created cat
:
And check how many words in each file again:
As we can see, both cat
and mouse
now have 2 words, because they are hard links to the same underlying inode, so the changes made to the content of it affected both files.
But how to remove the inode? When we delete a file it removes one link to the underlying inode. The inode is only deleted when all links to it have been deleted.
Unlike the hard link, a symbolic link (also symlink or soft link) is the nickname for any file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution.
The symbolic link is a separate file that exists independently of its target. If a symbolic link is deleted, its target remains unaffected. If a symbolic link points to a target, and sometime later that target is moved, renamed or deleted, the symbolic link is not automatically updated or deleted, but continues to exist and still points to the old target, now a non-existing location or file.
Let’s play with our cat and mouse again. First, remove the mouse hard link:
rm mouse
Second, create the symbolic link named mouse, that will point to a cat file:
ln -s cat mouse
Now we can check the inodes of both cat
and mouse
:
As we can see, that both cat
and mouse
files now pointing to a different inodes. That means that they are not connected.
Let’s calculate the number of words in our cat
and mouse
:
As we can see, the numbers are equal, because the mouse
link points to the cat
file, and when we called the wc
command on it, the operating system resolved the mouse
link, and calculated the number of words in the cat
file.
But what if we delete the cat? Let’s do it:
rm cat
Now let’s try to calculate the number of words for mouse
again:
As we can see, we’ve got an error: “No such file or directory”. We knew that our mouse
file is present in our current directory (ls | grep mouse
), so how should we interpret the error? Because we know that our mouse is a symbolic link which points to a cat file, and the cat file doesn’t exist, we should interpret this error as a broken link. But what if we don’t know, how to make sure? It’s simple: with the file
command:
As we can see, the outputted error in this case gives as the exact reason why command can not be executed.
That’s all for today, folks, I hope it was helpful.