LFCS Admin Exam preparation guide – Create and manage hard and soft links

LFCS Admin Exam preparation guide - Create and manage hard and soft links
LFCS Admin Exam preparation guide – Create and manage hard and soft links

LFCS Admin Exam preparation guide series, main page can be found here.

This post is part of Essential Commands from the domain competency list for the exam. The full list can be found in the link above paragraph or the Linux Foundation page here.

Today lets explore how we can create and how we use the hard and soft links. Hard and soft links are like shortcuts to the file you put on your desktop in GUI.

Hard link – is a direct pointer to the file, and the only file, must be store on the same file system and as long as the hard link exists the data will exist, hard link shares the same inode as the source file. To create a hard link we use ln command. We used the ln info.txt hardlinkinfo command to create the hard link to the info.txt file, when listing the content of the new link we can see that it contain the same data, by using the ls -li where i stands for inode we can see that both files share same inode value.

hard link creation example
hard link creation example

We can use hard links to protect files from accidental deletion, by creating the hard link in some other location on the file system, we can be sure that if the file was deleted, we could restore it by copying the hard link. Below we added some text to our info.txt file. Next, we removed the info.txt file, and we can see that when listing the content of the hardlinkinfo we can see the data. We can use the cp command to recreate the info.txt file form the hard link.

hard link use for file restoration
hard link use for file restoration

Soft Link (Symbolic Link) – redirect to the file more like shortcut or alias, we can create both to the file and the directory. It can be store in different locations like share drive and as well on a different file system. A soft link not sharing the inode with the file/directory has a unique one, if we delete the source for the link, the link is broken.

We have created a file on inside out directory with a crucial message: “Practice, practice before the exam!” :). Now we will create a soft link, same as hard link we will use the ln command with -s operator. The new link was created because we can see that the file has in description to which file it points. When we display the content of the soft link we can see it shows the same as the original file. We create a soft link outside the directory with the source file, and we can see in the link description the full path to the file. Now let us delete the source file because the file no longer exists. We can see that the soft link is broken.

soft link creation example
soft link creation example

Let us now explore how we can use soft links to point to the directory if you coming from GUI is basically like a shortcut on your desktop. First, we will run the same command but use the directory as a target. After creating, we can see that the link is pointing to the data directory. Because it is like a shortcut, we can use it to change directory, and we can see that the content is the same as the source directory.

soft link to the directory example
soft link to the directory example

That is all for today; remember, the best way to learn commands is by practicing. Thank you for reading.