Guide to adding Linux Symbolic Links

We shall see what is a symbolic link, the differences between a hard link and a symlink, how to create and remove both types, and their value in Linux.

Years ago, when I first started with Linux and wore a younger man’s clothes, one of the most challenging things for me to understand about Linux was the concept of symbolic links, and their use in the Linux environment.

Now, almost two decades later, I finally understand it. In this article, I’ll try to explain precisely what a symbolic link is, the differences between a hard link and a symbolic link, or symlink, how to create and remove both types, and their value in the Linux environment.

What is a Hard Link?

In Linux and Linux-like operating systems, virtually everything is a file.  With that in mind, in Linux, a file is just a link to an inode.  An inode is a data structure that keeps everything about a file separate from its name and the actual content.

Some Linux users like to think of a hard link as an extra name for an existing file.  So, when you create a hard link, you’re just associating two or more filenames with the same inode.

For users more familiar with Windows, a Linux hard link is much like a Windows hard link that you create with the DOS command:

C:/> mklink /h "link" "target"

That looks like a problematic concept.  Let’s see if we create a hard link in the Linux terminal; it makes things clearer.

How to create a Hard Link

We use the ln command to create link files in Linux.  The format for the ln command to create a hard link is:

# ln "source" "link"
# ln "/path/to/source" "/path to link"
# ln "target" "link"
# ln "target" "directory"

Where “source” is an existing file, and “link” is the file to create or our hard link.

So, in the following example, we are going to

Create our “source” file.

# echo 'This is the FOSS Linux Hard Link example' > FOSS

Create our “link” file.

# ln FOSS Linux

List our created “source” file (FOSS) and our “link file” (Linux).

# ls -li FOSS Linux
Creating a hard link in Linux.
Creating a hard link in Linux

If you look at our output from our ls command, we can see that the inodes match. We used the -i option specifically with our ls command so that we can see the inodes of our files, in this case, 9448255, for both.  Remember, with hard links; we’re “just associating two or more filenames with the same inode.”

Also, notice that the third column of our output shows the number 2.  That’s the total number of hard links to the file.  Here, 2.

Note that you cannot create hard links for a directory.  So, for example, you couldn’t create a hard link to refer to the directory /home/tut.  You can’t create hard links for directories and files on a different filesystem or partition, either.

What is a Symbolic Link?

Symbolic links or symlinks (sometimes called soft links) were much easier for me to digest than were hard links.  To get the gist of a symbolic link, think of a Windows shortcut.  That all a symbolic link is a pointer (or shortcut) to the actual file.  Unlike hard links, symbolic links aren’t two files referencing the same inode. Instead, the symbolic link is just a ‘shortcut’ to the actual file.

Let’s create an example of a symbolic link and explore what I mean.

How to create a Symbolic Link

Just like with a hard link, we use the ln command to create a symbolic link.  Only to create the symbolic link, we use the -s qualifier (ln -s) to specify that the link we are creating is a symbolic link.

Let’s create or symbolic link.  You’ll remember where we left off with our hard link files earlier.  We’ll build on that.

Our previously created hard links.
Our previously created hard links.

Let’s create our symbolic link file.

# ln -s FOSS Is_Great

Now, we’ll look at our files.

# ls -li FOSS Linux Is_Great
Our directory updated with our symbolic link.
Our directory updated with our symbolic link

Looking at our updated output from our modified ls command, we can see nothing’s changed with our hard links, but our symbolic link is now included.  The inode of our new file differs from our two hard links, and the second column starts with an l.  The l denotes that the file is a symbolic link.  You can see that the newly created symbolic link (or shortcut) points to the file it is a symbolic link for.  Here, FOSS (Is_Great > FOSS).

Conclusion

Hopefully, after reading our article, you have a better understanding of hard links and symbolic links or symlinks. If you have trouble, I cannot recommend the ln man page enough.

ln man
ln man

Should you have questions, please ask in the comments below, and we will get back in touch with you.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

22,858FansLike
446FollowersFollow
16SubscribersSubscribe

Latest Articles

Suggested

How to install and run Powershell on Fedora Linux

PowerShell built upon the .NET Core framework is a powerful open-source command-line shell developed and maintained by Microsoft. It is a cross-platform (Windows, macOS, and Linux) automation and configuration tool that works well with your existing tools. It includes a command-line shell and an associated scripting language.

Featured

More Articles Like This