The difference between Linux hard and soft links

Jack Wallen explains the difference between Linux symbolic and regular links in terms that anyone can understand.

Top 5 things you need to know about Linux vs. Windows

Your data center might well be filled with Linux servers. Or maybe there's just one or two among your server farm. No matter how much you lean on Linux for your business, there are certain fundamental ideas you need to understand, if you want to keep those servers running at peak efficiency. One such element you must understand is that of links. A link is a special kind of file that points to another file or location on your system directory. Links make it easy to point a user or application to one location that might refer to a much more convoluted path. Say, for instance, you have a path housing user data at /usr/local/data/source/appsource/users. The last thing you want to do is to share out that complete path every time or require end users to remember that path. Instead, you could use a link, such that users only need to go to /userdata.

On the surface, it's that easy. However, there is one aspect to links you must consider: Regular or symbolic (often referred to as hard or soft). That's right, there are two different types of links on a Linux system and it's important to understand the difference between the two.

Let me explain. However, I'm going to do this in simplest terms. The difference between the two types of links is actually a bit more complicated than what I am going to lay out, but I want to make this such that anyone can immediately understand the concept. Capisce?

Symbolic links

Symbolic links are probably what you will most use on your Linux servers. A symbolic (or "soft") link is a file that acts as a shortcut to another location on your file system (similar to an application launcher in a menu). If you were to create a symbolic link named ~/shortcut that pointed to /usr/local/data/user, if you issued the command ls ~/shortcut, the output of the command would actually be listing out the contents of /usr/local/data/user (of course, if I'm being technical, it actually points to a different inode ... but that's another story for another day). If you created a new file in ~/shortcut, called test, that new file would actually be saved in /usr/local/data/user. Also, if you create a new file in /usr/local/data/user, it would immediately be available in ~/shortcut.

But how do you create that symbolic link? It's actually quite simple. We'll stick with our example. To create the ~/shortcuts symbolic link, pointing to /usr/local/data/user, the command would be:

ln -s /usr/local/data/user ~/shortcut 

The -s flag denotes this is a symbolic link.

Once you issue the above command the symbolic link will be created. Issue the command ls ~/ to see the new link in your directory (Figure A).

Figure A

Figure A

Our newly created link.

To remove the link, you would only need issue the command:

rm ~/shortcut

It should be noted that, in order for a user to successfully work with a symbolic link, that user would need permission on the target folder. So if a user doesn't have write permission to /usr/local/data/user, they won't be able to write files to the symbolic link. So either before or after you create the symbolic link, make sure to test if users have the necessary permissions to the target directory.

As with directories, you can create a symbolic link to a single file as well. Say you have /usr/local/data/user/test and you want to create a symbolic link to it in your home directory. The command for that is similar:

ln -s /usr/local/data/user/test ~/test

There is one instance where symbolic links can be a bit tricky. Say you have created the symbolic link ~/test which points to /usr/local/data/user/test and you move the /usr/local/data/user/test to /usr/local/data/user/TEST/test. If you do that, the symbolic link will remain, but it won't be pointing to the target file. The link will have been broken. If you make changes to ~/test, those changes won't reflect in /usr/local/data/user/TEST/test. Also, if you delete the original file, the symbolic link will remain and (again) not point to anything. Let's see what happens. After creating a symbolic link, I moved the target file from one directory to another. The original output of ls -l shows a working symbolic link, whereas the output of ls -l, after having moved the target file, shows a broken symbolic link (Figure B).

Figure B

Figure B

Our broken symbolic link appears in red.

The best thing about symbolic links is that they can point to anything (files or directories), anywhere on the system ... and even span across files systems. The biggest issue with symbolic links is that there is a slight space and speed penalty. Symbolic links add an extra layer of indirection on top of normal file access, such that the kernel has to navigate the link when you open the file. This adds a tiny amount of time to the process. If your server is having to access thousands of files, that time can certainly add up.

Regular links

Regular (aka "hard") links are a different beast altogether. Where a symbolic link creates a shortcut to another location, a regular link is like creating a mirror of one location to another location. So if you have the file /usr/local/data/user/test and you create the hard link ~/test, the contents of /usr/local/data/user/test now exists in two places. This means if you delete target file, the shortcut file will remain intact. That's not the case with symbolic links (if you delete the target file, the symbolic link will fail).

Before I show you how to create a hard link, know that you cannot create regular links to directories, only files. If you try to create a regular link to a directory, you might find yourself receiving an error for your effort (Figure C).

Figure C

Figure C

Hard links to directories not allowed.

You can, however, create a hard link to a specific file. Regular links are created in the same fashion as symbolic links, only without the -s flag. So to create our regular link ~/test1 that points to the original file /usr/local/data/user/test1, the command would be:

ln /usr/local/data/user/test1 ~/test1

If we edit either file, the changes will reflect in both. If we delete either file, the other will remain intact.

The best thing about regular links is that there is no speed or size penalty when using them. The biggest problem with regular links is that they cannot span across file systems.

Your choice

What you use will depend upon your needs. Chances are, you'll mostly use symbolic links, as they are more flexible. But when speed is crucial, consider regular links first.

Also see