In this article by Oliver Pelz, the author of Fundamentals of Linux, you’ll take a look at what Linux file links are and how to work with them.

Linux File Links

As you may already know, files are stored on the hard disk. In a Linux filesystem, the filename and the data are two separate concepts and are not stored together. A general structure is shown in the following diagram:

 Linux File Links

Connecting a filename to the actual data is managed by the filesystem using a table or data structure, which is called a title allocation table. In the Linux filesystem, an Inode is the actual entry point to a specific file's data on the hard disk. To simplify, you can just consider that the Inode represents the actual data of a file. The filesystem management now ensures that every normal file, upon creation, has one link entry in its allocation table to connect the actual filename to the Inode on the hard disk. Such a link is also called a hard link. The original filename to the Inode relationship is also linked using a hard link. Now, the cool thing about the Linux filesystem is that you can create additional hard links to an existing Inode, which is like having alternative names for a file.

One of the drawbacks of a hard link is that you cannot differentiate a hard link from the original filename or the Inode. This can cause problems and side effects because if you change the original file's content, the hard link's content will be changed as well. Another limitation of hard links is that you can only define them for Inodes, which are on the same partition as the hard link should go. Moreover, you cannot create hard links on directories. You can only create them on normal files. To solve these limitations of hard links, you can use soft links, also known as symbolic links. These are links that you can use almost all the time in your everyday work as a Linux system administrator. Hard links also have their special use cases, for example, to create file backups, but are only used very rarely by a Linux user.

Recommended Read : Important BASH tips tricks for Beginners

A symbolic link is a link to the filename and not to the Inode. Symbolic links also don't have the boundary that they must be on the same partition as the original file. You can also create symbolic links on a directory. The main drawback is that if you delete or move the original file, you will have a broken symbolic link without further warning, which can also create some bad side effects. The main use cases and power of symbolic links are referencing configuration files or dynamic library versions in the Linux filesystem. Using Linux file links can save a lot of disk space because no actual data needs to be copied, and they are very effective for quickly testing out such things as alternate configuration files for services.

Also Read : Linux Commands for Beginners (Part 1)

Linux File Links are managed by the ln command. The basic syntax is ln [OPTION], then the filename you want to create a link on, and finally the link name. To create a hard link to a file called fileX in your home directory, use the following code:

 Linux File Links

As you can see, there's no way to differentiate the additional hard link from the original one. You can also create multiple links on the same file. To delete a hard link, use the rm command. There's a maximum number of Inodes on every filesystem, or you can just simply say files, which you can display using df -i. If you use the mount command, you will see that the tmp filesystem for the user is on a different partition from the home directory, which is in turn, on the root partition as shown in the following screenshot:

 Linux File Links

So, the next command, ln ~/folderABC ~/folderABC_link, will fail because it is not allowed to create hard links between partitions. Also, you cannot create a hard link on a directory, and changing the origin of the file's content will change the hard link's file content as well. This can create some bad side effects. To create a symbolic link, use the ln -s option:

 Linux File Links

As you can see, it's easy to show a file as a symbolic link marked with the arrow. To create a symbolic link of a file in another directory, preserving the original file's name, you can use ln -s /etc/passwd. This creates a symbolic link of the /etc/passwd file in the current directory under the same name, passwd. To delete a symbolic link, use the rm command; the original file will not be touched. You can also create a symbolic link on a directory. If you delete the original file that the symbolic link is pointing to (fileX here), the symbolic link will break. This can get problematic, which is denoted here with the blue color:

 Linux File Links

You’ve now learnt how to work with Linux File Links. If you want to learn more about Linux, you can explore Fundamentals of Linux, a step-by-step guide filled with real-world examples to help you develop a solid understanding of the important command-line tools and utilities in Linux.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of your hard earned money: [paypal-donation]

Linux TechLab is thankful for your continued support.