Hard Versus Soft Links in Linux: Understanding the Linux Symbolic Link

Hard Versus Soft Links in Linux: Understanding the Linux Symbolic Link
Page content

If you are a newbie to Linux or any Unix-like operating system, especially to working on a command line, I would highly recommend becoming familiar with a distribution, basic command line work, and the file system before attempting to work with the Linux symbolic link.

I believe it’s important to first acquire a good understanding of the subject of hard versus soft links in Linux before actually creating either of the two. Although this might sound a little intimidating, you can grasp it if you take all the time you need to comprehend the material and practice with caution. I will be covering some things you should not do to avoid frustration and confusion as you learn.

You should already be familiar with the basic file system of Linux distributions and know that almost everything under a distro is a file; network interfaces are not files. Keeping this in mind, you can think of those files being linked, but what you may not have thought of before is the fact that some of those links are symbolic and others are “real” or hard links. There are major differences between the Linux symbolic link, also known as a “symlink” or a “soft” link, and the hard link. This should be stressed because merely realizing that they’re not the same thing is essential to your understanding.

Every file, which is often referred to as a “filename,” points to what is called an “inode.” The inode is the absolute location of a file or filename. The word absolute is the key as you will soon see that when creating these links you can’t rely on relative locations; if you do, you will get error messages. You can point more than one filename (file) to the same inode resulting in a hard link. Each of the files shares the exact same content and attributes. If you modify one, you modify the other. Why would anyone want to do that? The creation of hard links is one method of backing up a file. It’s certainly not among the best methods in my opinion, but it is at your disposal. At the moment, we’re not talking about the Linux symbolic link, but rather the subject of hard versus soft links in Linux. Learn what a hard link is before attempting to create the Linux symbolic link.

Those who use hard links as a way of backing up a file on the same disk usually have the two files in two different locations. Although it’s true that when you modify one file you modify the other, if you delete one, you do not delete the other. This is where the back-up benefit lies. Again, it’s not perfect, but it’s better than no back-up plan at all and works well for those who are careless about implementing a back-up plan for data. The Linux symbolic link is not used to back up a file. The screen shots on the second page show how I created a hard link; in other words, how I’ve pointed two filenames to one inode. You should not have to become root for this.

I opened a terminal and used the ls command to list files and directories, the cd command to change to the Desktop directory where I created an empty file named hardlinkA.txt using the touch command. I then used the mkdir command to create a practice directory called Another Location. The back slash is used to escape the space; I then changed to the newly created directory. Next, I used the ln command for the creation of the hard link to have the filename hardlinkB point to the same inode as hardlinkA.txt. You type the command first (ln), then you input the location of the file followed by a space and the name of the link. Finally, I used the ls command to report. The filename hardlinkA.txt appears in the list; after the report on the file permissions you see the number “2” indicating the number of hard links.

There’s extensive use of symlinks under Linux for tasks such as library versioning. The Linux symbolic link allows you to make a file appear to exist in a different location. Unlike a hard link, a symlink is not a true file holding data so it is always very small in file size. Also, the filename to which the symlink points cannot change because symlinks don’t actually keep the contents of files. They are just “symbols” of or “pointers” to another file.

To create the Linux symbolic link you use the same ln command, but you must also pass it the -s parameter. The basic syntax is: ln -s . If you fail to pass the -s parameter, you will default to the creation of a hard link–big difference. If you use the ls -l command to report, you will see that the system uses an arrow as confirmation of where the links goes. You can see my demonstration of creating a symlink on the command line and the report that I ran on it to confirm. Simply click on the screen shot below. I wouldn’t recommend practicing with symlinks by pointing them to system files, but rather by creating practice target files. Also, except for demonstrations purposes, you don’t want to change the names of files to which you have a symlink. Likewise, you might want to keep note of soft and hard links that you create for back-up purposes, practice and any other reason.