Hard Links and Symbolic Links

Paula Louvani
3 min readFeb 6, 2021

--

Links is a painting by Betty-Anne McDonald

Before talking about hard and symbolic links we need to know that Linux files are very simple, they only contain the file name and the Inode number but contain no metadata about themselves. So what is INODE?

An inode is an Acronym of Index Node, every file and directory in our system has an Inode that stores metadata about the file, like timestamps, as well as owner and permission data. The directories Inode contain an entry link for itself, its parent, and each of its children.

When we use the command ls -l what we see is the information that Inode provides. The inode contains the size of a file in bytes, the location in the disk where Linux needs to read the file; permissions; time of access, modification, and creation; and also the reference count, which tells us how many hard links lead to the Inode. Inode does not contain the content of a file nor the name of a file.

The command ls -i prints the Inode number of each file. And to see exactly what the Inode contains we can use de command stat name_of_file.

“Stat” command for a directory
“Stat” command for a file

So with that in mind, we can have different names of files that point to the same Inode, these name files are hard links. These kinds of links have the same size as the original file. If we delete the original file the hard links will keep storing the same data. If we change one hard link we change all hard links that point to the same Inode.

These hard links are useful to easily access files, programs, and scripts in a different directory from that of the original file. Writing the name of a hard link lets the program or a sequence of commands be executed in the same way as the original file. Hard links cannot be created for directories, only for files.

To create a hard link we use the command “ln file.1 file.2”

Another kind of link is the Symbolic Links, also called soft links, these are files that point to a hard link, not to the Inode. For that reason, its size is less than the original link. The Inode number of the symbolic link is different from the original.

If we delete the original file the symbolic link becomes useless. Symbolic links do not store the data of an original file only the location of it. Symbolic links can be created for directories and files.

To create a symbolic link we use the command “ln -s file.1 file.2”

--

--

Paula Louvani
Paula Louvani

Written by Paula Louvani

0 Followers

Studying at Holberton School

No responses yet