Home Learn Linux Guide to adding Linux Symbolic Links

Guide to adding Linux Symbolic Links

by Travis Rose
Published: Last Updated on
Linux Symbolic Links

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.

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.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.