Linux Stall
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials
No Result
View All Result
Linux Stall
No Result
View All Result
Home Tutorials

Softlinks vs. Hardlinks: A Quick Explanation

Chankey Pathak by Chankey Pathak
July 23, 2020
in Tutorials
43 2
0
softlinking linux
15
SHARES
758
VIEWS
Share on FacebookShare on Twitter

I got this question in an email the other day, and I realized it’s something many people might be unfamiliar with. In Linux, there are two types of file links, hardlinks and softlinks. Here, I’ll give a quick explanation of what these types of links are, and when you should use them.

What is a softlink?

Softlinks, also called symlinks, are the easiest to understand, especially because you’ve probably already used them. A softlink fills the same role as a Windows shortcut. Simply put, a softlink is a file that points to another file. When you create a softlink, you are creating a new file that exists only as a pointer to a file elsewhere on the system.

All links are created with the ln command, using the -s flag if you want a softlink (if you don’t use -s, you’ll get a hardlink, which I’ll talk about in a second). The syntax of the command is:

ln -s [target] [link name]

For example, the following command will create a softlink to /usr/bin/firefox called firefox (in my “Desktop” directory):

ln -s /usr/bin/firefox ~/Desktop/firefox

You can see the softlink’s target by using the ls -l command. You can also use this command to detect broken softlinks:

softlink

Now that we understand softlinks, let’s talk about hardlinks.

What is a hardlink?

Whereas a softlink is a new file that points to an already-existing file, a hardlink is another instance of the original file.

A diagram is the easiest way to explain what that means:

Softlink:

softlinking linux

Hardlink:

hardlinking linux

You can click on either of the images for a larger version. What it explains is that, when you create a hardlink, you are creating another pointer to the data location on disk, not a pointer to the existing file. That means that editing a hard link of a file is equivalent to editing the original instance of the file.

To drive the point home: a softlink is a shortcut to an existing file, whereas a hardlink is a reference to a location on disk (or, more accurately, a location in the filesystem). This means that the concept of a shortcut, a link pointing to another file, doesn’t make sense for hardlinks. But, what does make sense is asking how many references exist to a given location on disk (how many hardlinks exist for a file), which you can see by running the ‘stat’ command:

stat /path/to/file

So for example, in this screenshot I’ve created a file and then built three hardlinks to it. When I run the ‘stat’ command on any of the files, it will show that there is a reference count of 4:

hardlink in terminal

When to use softlinks

There are two major limitations of hardlinks. In these cases, you must use a softlink:

1. A link across filesystems

Because a hardlink is a direct reference to the underlying filesystem, you can’t hardlink across filesystems.

2. Linking to a directory

You can’t use a hardlink to link to a directory.

In the cases listed above, you must use a softlink because a hardlink simply won’t work. There are also cases where softlinks are preferable to hardlinks, even though either will work. For example, you’d probably want to pick a softlink when you want to create a shortcut especially when the destination of that shortcut might change in the future. For example, if you have a launcher to a beta version of an application, you may wish to use a softlink so you can easily change the target of the link between versions.

In most of the remaining cases (creating a link to a file on the same filesystem), hardlinks can be preferable for the following major reasons:

1. Performance

There is a slight performance boost to be gained from using hardlinks. This is because since a hardlink references a spot on disk, rather than referencing another another file (which then references the actual spot on disk you want), there is one less disk seek involved in hardlinking.

2. Storage space

Hardlinks don’t take up any additional space, since they are simply a reference to an already existing space of disk. Creating a softlink is creating a new file, and will consume a small amount (usually 4KB) of space on your filesystem.

Additionally, hardlinks are preferable because a softlink is vulnerable to losing the ‘master’ instance of a file (the file to which all the softlinks point). If the original file is deleted, then all softlinks to it become invalid. With a hardlink, you can delete any of the instances, including the original, and as long as at least one instance exists the file will remain on the system.

Tags: softlinkTutorials
Previous Post

Drastically Speed up your Linux System with Preload

Next Post

Port Forwarding in OpenSSH

Chankey Pathak

Chankey Pathak

Data Scientist at Morgan Stanley. I've been using Linux since past 12 years. I plan to share what I know about Linux in this blog.

Related Posts

case of computer server workstation technology wireless vector illustration
Tutorials

Port Forwarding in OpenSSH

July 25, 2020
write chroot read stat
Tutorials

Strace in Linux: History, Structure and Usage

July 21, 2020
case of computer server workstation technology wireless vector illustration
Tutorials

Brocade: Health Check Commands

July 12, 2020
A programmer coding
Tutorials

Using Basic Loops In Bash Scripts

July 12, 2020
uname
How to

How to find Linux distribution name and version?

June 22, 2014
Setting up your Linux box to serve as a DHCP server or a DHCP client
Tutorials

Setting up your Linux box to serve as a DHCP server or a DHCP client

March 28, 2012
Next Post
case of computer server workstation technology wireless vector illustration

Port Forwarding in OpenSSH

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Terms and Conditions
  • Contact Us
  • About Us

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

No Result
View All Result
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In