Hard links and symbolic links — the differences.

Caroline
2 min readFeb 2, 2021

Every file on Linux at the point of creation has a hard link. The title of the file and its contents stored in the system are linked. Additional links can be created. You can create a second file that points to the exact same contents as the first file. Therefore a hard link is an extra file that points to the exact contents of an existing file. You can also create hard links for other hard links, but you cannot create hard links for directories.

Why use hard links? Well for starters they allow the user to quickly access a file’s contents from a different directory. You can also execute a program via a hard link.

To create a hard link, you use the “ln file_name hardlink” script. “ln” is the command to create the link, “file_name” is the name of the file you want to copy the contents of, and “hardlink” is the name of the new file.

If you use the ls command with -i, you can get a list in your terminal of all the files along with their inodes. Inodes is a data structure in Unix-style systems. You can find more about Inodes here: https://www.howtogeek.com/465350/everything-you-ever-wanted-to-know-about-inodes-on-linux/. If two files have the same inodes, that means they are linked.

The other type of link you can create is a soft link. A soft link is a special type of file that points to another file. Unlike the hard links mentioned above, symbolic links do not store any date inside. It’s simply just a pointer. Which means you can use it to point to directories. But unlike hard links, that retain the contents of a file when the original is deleted, symbolic links do not retain the contents of the target file.

To create a symbolic link, you use the ln command with the -s option, “ln -s file_name softlink1”. With softlink1 meaning the name of the new softlink. Unlike hard links, softlinks and their target file do not share the same inodes.

--

--

Caroline
0 Followers

Holberton School Programming Student