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.

Travis Rose
Hi, I'm M Travis Rose, a contributor to FOSS Linux. I have over thirty years of experience in the IT arena, at least fifteen of which has been working with Linux. I enjoy converting existing Windows users to the wonderful world of Linux. I guess you could call me a Linux-evangelist. Long live Linux!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,241FansLike
399FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

“I am because you are,” is the themed meaning behind the famed Ubuntu operating system. Moreover, this mindful phrase is practical because it continues to lure more individuals into the Ubuntu universe. Because great power beckons great responsibility, Ubuntu is stepping up. It realizes that different users will want to use the Ubuntu operating system software differently.
The Ubuntu Cinnamon Remix brings together Linux Mint's Cinnamon desktop with the Ubuntu Core. While some users are welcoming the new flavor of Ubuntu with open arms, others are scratching their heads, wondering where it fits in.

How to clone hard disk on Linux using Clonezilla

Disk cloning refers to the process of copying data from one disk to another, thus creating a one-to-one copy of the drive. Technically, this process is possible using the copy-and-paste method.

6 ways to find out your Linux file system type

Any Operating system in the market whether its Windows, Linux, Unix, macOS, and any other, must be able to access and manage files and data on storage devices.

5 Best Music Players for Linux

For sure, there will be Linux users out there who cannot live without music. If you're one of those people, you would know how important it is to have a good music player installed on your computer system. In this article, we present to you the five Best Music Players for Linux, which will hopefully be able to satisfy each of our readers' needs.

10 Linux Based Mini PCs to Buy in 2020

It won't be wrong to say that mini PCs have all the potential in the world to take over the computer market shortly. Not only do they save a lot of space on your computer desk but also work in a very power-efficient manner while also causing less noise. Although they could be a tad more expensive than regular desktop PCs, they will actually save you some money in the long run.