Soft links vs Hard links in Linux

Featured image by Travis Saylor via Pexels.

What is a soft link? What is a hard link? If you’re new to Linux and coming from Windows, you can think of these as what Windows calls “shortcuts” (although they are a little bit different and I’ll get into that). Here’s a way to think about it: a symbolic link or a soft link is a pointer to a file on the file system. A hard link or just a link is a mirror copy of the file. Let’s look at some examples so hopefully this will start to make sense.

Soft links

The most common type of link you’ll encounter or use is the soft link or symbolic link. Symbolic links are able to cross file systems and can point to directories, as well as files that don’t even exist. They can also point to other symbolic links allowing you to end up with an infinite loop.

Here’s a look at soft links. I will create a directory called “example” in my home directory and I’ll enter that directory.

mkdir example cd example

Inside this directory, I’ll create a simple text file, but in reality this could be anything like a configuration file (for example).

echo "Hello Linux Workbench!" >hello.txt

And here’s what the contents of the file show when we cat the file:

cat hello.txt
Hello Linux Workbench!

Now, I am going to create a soft link to hello.txt called workbench.txt:

ln -s hello.txt workbench.txt

So to create a link, we use ln along with -s to indicate a symbolic link. If you’re familiar with other tools like mv or cp, the syntax should look familiar. In this case, it’s the original file and then the name of the link.

Here’s a look at the terminal and an output of ls -la

Notice how workbench.txt is a different color and has an arrow pointing to hello.txt. This tells us that this is a symbolic link. If we cat out workbench.txt, it returns the contents of hello.txt:

cat workbench.txt Hello Linux Workbench!

We can actually work with workbench.txt as if it were hello.txt.

If you want to remove this link, you can simply run the rm command:

rm workbench.txt

And just like that, the link is gone!

As I previously showed you, you can easily find soft links and where they are pointing to simply by using ls -la.

Hard links

Unlike soft links, hard links work a little differently. These links cannot cross file systems and they can only reference regular files, not directories or special files. They also mirror the original file, including inode and file permissions. If you delete the original file, the hard linked file still exists. Let’s take a look.

Just like our soft link example, I am going to create another hello.txt file:

echo "Hello Linux Workbench!" >hello.txt

And here’s what the contents of the file show when we cat the file:

cat hello.txt
Hello Linux Workbench!

Now, I am going to create a hard link to hello.txt called workbench.txt:

ln hello.txt workbench.txt

Notice that I just called the ln command without specifying any attribute. The default action of ln is to create a hard link. Now lets see what happens when we do a ls -la:

Isn’t that interesting? We have a hello.txt and a workbench.txt file. Both have the same date and time along with file permissions. Are these two different files? Let’s check with ls -lia which will show the inode number.

Well look at that! hello.txt has inode number 17385881 and workbench.txt also has the inode number 17385881. These are technically the same file! We can prove this further by editing workbench.txt. I’ll add a new line of text to workbench.txt only:

echo "Learning links is fun!" >>workbench.txt

Here’s what happens when I cat workbench.txt:

cat workbench.txt  Hello Linux Workbench! Learning links is fun!

What do you think happened to hello.txt? Does it only contain “Hello Linux Workbench!”? Let’s find out!

 cat hello.txt  Hello Linux Workbench! Learning links is fun!

Nope! It has been updated with the same content as workbench.txt! Remember, they have the same inode number. This means they are the same file. What happens if I delete hello.txt?

As you can see, workbench.txt remains. And you can also see it retained the same inode number as hello.txt – 17385881 in this example.

Finding hard links is harder (no pun) than soft links, but the only real way to determine if a hard link exists is to check the inode number.

Practical examples

You may be wondering why you would want to use either soft links or hard links. Consider this practical example. You have a website on a server and you want to store large files on a different filesystem. This could be a network file system so you don’t have to store these large files on the local server. You could create a soft link from your document root that has a directory called “downloads” and this links to “/mnt/nfs/downloads/” and this is where all the large files are located. When a visitor goes to your website and the downloads folder, for example:, they will really be downloading that file from /mnt/nfs/downloads/

A more common example is running applications. For example, you may install a program in /opt/program-v1.0/ but you’ll instead create a link /opt/program/ that points to the specific version. This way you can easily upgrade the application and reference it elsewhere in the system using a single path. When you update this program to a new version, you don’t need to remember all the places you referenced it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.