In this tutorial, we are going to discuss what are hard and soft links with syntax and how we can understand Hard and Soft Links on Linux easily.
In case, you are wondering how you can generate a shortcut on a Linux system, then this tutorial can be the perfect answer for you all.
Are you Ready to Start learning about Understanding Hard and Soft Links on Linux? here you go.
- What Will You Learn?
- How does storage work on a Linux system?
- What is Soft Link And Hard Link In Linux?
- Hard or soft?
- What is the difference between copying and creating a hard link?
- Manipulating links on a Linux system
- How to find links on a filesystem
- Quick facts about links on Linux
This section is completely about the stuff that is provided in this tutorial about the topic we are going to discuss. It helps you to know a bit earlier about what you are going to learn:
- How storage works on a Linux system and what inodes are exactly?
- What hard and soft links are, given what you just learned before
- How copying differs from creating links to your files
- How to create links on a Linux system
- How to find hard and soft links: all the commands that you should know.
- Some of the quick facts about hard and soft links
That’s a long program, so without further ado, let’s see how data is organized on your Linux system and what inodes are?
- Monitoring Linux Logs with Kibana and Rsyslog
- The Definitive Guide to Centralized Logging with Syslog on Linux
In order to understand what hard and soft links are, you need to have some basics on how data is stored on your system.
In a computer, data are stored on a hard drive.
Your hard drive has a capacity, let’s say 1 TB, and it is divided into multiple blocks of a given capacity.
If I launch a simple fdisk command on my system, I am able to see that my device is separated into sectors of 512 bytes.
That’s a lot of sectors, as my hard drive has a capacity of almost 32 GBs.
Now every time that I create a file, it will be stored on a block.
But what if the file size exceeds 512 bytes?
You guessed it, my file will be “fragmented” and stored into multiple different blocks.
If the different pieces of your file are physically far away from each other on the disk, the time needed to build your file will obviously be longer.
That’s why you had to defragment your disk on Windows systems, for example, you were essentially making related blocks closer.
Luckily for us, those blocks have addresses.
- Complete Node Exporter Mastery with Prometheus | Monitoring Linux Host Metrics WITH THE NODE EXPORTER
- Syslog: The Complete System Administrator Guide
- Fdisk Command in Linux (Create Disk Partitions)
Your Linux system does not have to read the entire disk to find your file, it will cherry-pick some addresses that correspond to your file data.
By using inodes.
Inodes are essentially identification cards for your file.
They contain the file metadata, the file permissions, the file type, the file size but most importantly the physical address of this file on the disk.
Without going into too many details, your inode will keep references to the locations of your different file blocks.
So one inode equals one file.
That’s the first layer of your file system, and how references are kept to the underlying data.
However, as you probably noticed, filenames are not part of the inode, they are not part of the identification card of the file.
On a Linux system, filenames are kept on a separate index.
This separate index keeps track of all the different filenames existing on your system (even directories) and they know the corresponding inode in the inode index.
What does it mean?
It essentially means that you can have multiple filenames (say “doc.txt” and “paper.txt”) pointing to the same exact file, sharing the same content.
Now that you learned about the filenames index, let’s add another layer to our previous layered architecture.
With this schema, you have a basic understanding of how files are organized and stored on your filesystem, but more importantly, you will be able to understand what hard and soft links are.
Let’s start with softs links as they are probably the easiest to understand.
Soft links, also called symbolic links, are files that point to other files on the filesystem.
Similar to shortcuts on Windows or macOS, soft links are often used as faster ways to access files located in another part of the filesystem (because the path may be hard to remember for example).
Symbolic links are identified with the filetype “l” when running a ls command.
They also have a special syntax composed of the link name and an arrow pointing to the file they are referencing.
In this case, the file “shortcut” is pointing to the file “file.txt” on my filesystem.
Have you paid attention to the permissions?
They are set to “rwx” by default for the user, the group, and the others.
However, you would be constrained by the permissions of the file if you were to manipulate this file with another user.
So why did we talk so much about inodes in the first section?
Let’s have a quick look at the inode of the file and the inode of the shortcut.
$ stat shortcut File: shortcut -> file.txt Size: 3 Blocks: 0 IO Block: 4096 symbolic link Device: fc01h/64513d Inode: 258539 Links: 1 $ stat file.txt File: job Size: 59 Blocks: 8 IO Block: 4096 regular file Device: fc01h/64513d Inode: 258545 Links: 2
The inodes are different.
However, the original file inode is pointing directly to the file content (i.e the blocks containing the actual content) while the symbolic link inode is pointing to a block containing the path to the original file.
The file and the shortcut share the same content.
It means that I was to modify the content of the shortcut, the changes would be passed on to the content of the file.
If I delete the shortcut, it will simply delete the reference to the first inode. As the file inode (the first one) still references the content of the file on the disk, the content won’t be lost.
However, if I were to delete the file, the symbolic link would lose its reference to the first inode. As a consequence, you would not be able to read the file anymore.
This is what we call a dangling symbolic link, a link that is not pointing to anything.
See the red highlighting when I deleted the original file?
Your terminal is giving visual clues that a symbolic link is a dangling symbolic link.
So what’s the size of a symbolic link?
Remember, the symbolic link points to the path of the original file on the filesystem.
In this example, I created a file named “devconnected”, and I built a shortcut to it using the ln command.
Can you guess the size of the shortcut? 12, because “devconnected” actually contains 12 letters.
Now you have a good understanding of what soft links are.
Hard links to a file are instances of the file under a different name on the filesystem.
Hard links are literally the file, meaning that they share all the attributes of the original file, even the inode number.
Here’s a hard link created on my system.
It is sharing the same content as the original file and it is also sharing the same permissions.
Changing permissions of the original file would change the permissions of the hard link.
Let’s have a quick look at the original file inode and the hard link inode.
$ stat hardlink File: hardlink Size: 59 Blocks: 8 IO Block: 4096 regular file Device: fc01h/64513d Inode: 258545 Links: 2 $ stat file.txt File: file.txt Size: 59 Blocks: 8 IO Block: 4096 regular file Device: fc01h/64513d Inode: 258545 Links: 2
As you probably noticed, the inodes are the same, but the filenames are different!
Here’s what happens in this case on the filesystem.
When you are creating a symbolic link, you are essentially creating a new link to the same content, via another inode, but you don’t have access to the content directly in the link.
When creating a hard link, you are literally directly manipulating the file.
If you modify the content in the hard link file, the content will be changed in the original file.
Similarly, if you modify the content in the original file, it will be modified in the hard link file.
However, if you delete the hard link file, you will still be able to access the original file content.
Similarly, deleting the original file has no consequences on the content of the hard link.
Data are definitely deleted when no inodes point to it anymore.
You won’t find a clear answer to this question. If the type that suits your special situation can be the best link. While these concepts can be tricky to memorize, the syntax is pretty straightforward, so that is a plus!
To keep the two links easily separated in your mind, I leave you with this:
- A hard link always points a filename to data on a storage device.
- A soft link always points a filename to another filename, which then points to information on a storage device.
With the concepts that you just learned, you may wonder what’s the difference between copying a file and creating a hard link to the file.
Don’t we have two files in the end, with the same content?
The difference between copying and hard linking is that hard-linking does not duplicate the content of the file that it links to.
When you are copying a file, you are essentially assigning new blocks on the disk with the same content as the original file.
Even if you share the same content with hard-linking, you are using disk space to store the name of the original file, not the actual content of the file.
Diagrams may explain it better than words.
Here’s what copying a file means.
See how it differs from hard-linking?
Now that you know how copying files differ from hard linking to files, let’s see how you can create symbolic and hard links on a Linux system.
To create a symbolic link, you need to use the ln command, with a -s flag (for symbolic).
The first argument specifies the file you want to link to.
The second argument describes the name of the link you are about to create.
$ ln -s file shortcut $ ls -l -rw-rw-r-- 1 schkn schkn 0 Aug 14 20:12 file lrwxrwxrwx 1 schkn schkn 4 Aug 14 20:12 shortcut -> file
You can also create symbolic links to directories.
$ mkdir folder $ ln -s folder shortcut-folder $ ls -l drwxrwxr-x 2 schkn schkn 4096 Aug 14 20:13 folder lrwxrwxrwx 1 schkn schkn 7 Aug 14 20:14 shortcut-folder -> folder/
To remove existing symbolic links, use the unlink command.
Following our previous example :
$ unlink shortcut $ ls -l -rw-rw-r-- 1 schkn schkn 0 Aug 14 20:12 file
You can also simply remove the shortcut by using the rm command.
Using the unlink command might be a little bit safer than performing an rm command.
$ rm shortcut $ ls -l -rw-rw-r-- 1 schkn schkn 0 Aug 14 20:12 file
A hard link is created using the ln, without specifying the s flag.
$ ln file hardlink $ ls -l -rw-rw-r-- 2 schkn schkn 0 Aug 14 20:12 file -rw-rw-r-- 2 schkn schkn 0 Aug 14 20:12 hardlink
Again, you can use the unlink command to delete a hard link on a Linux system.
$ ln file hardlink $ unlink hardlink $ ls -l -rw-rw-r-- 2 schkn schkn 0 Aug 14 20:12 file
Now that you know how to create links, let’s see how you can find links on your filesystem.
There are multiple ways to find links on a Linux system, but here are the main ones.
Using the find command
The find command has a type flag that you can use in order to find links on your system.
$ find . -type l -ls 262558 0 lrwxrwxrwx 1 schkn schkn 7 Aug 14 20:14 ./shortcut-folder2 -> folder2/ . . . 262558 0 lrwxrwxrwx 1 schkn schkn 7 Aug 14 20:14 ./shortcut-folder -> folder/
However, if you want to limit searches to the current directory, you have to use the maxdepth parameter.
$ find . -maxdepth 1 -type l -ls 262558 0 lrwxrwxrwx 1 schkn schkn 7 Aug 14 20:14 ./shortcut-folder -> folder/ 258539 0 lrwxrwxrwx 1 schkn schkn 3 Jan 26 2019 ./soft-job -> job
Finding links that point to a specific file
With the lname option, you have the opportunity to target links pointing to a specific filename.
$ ls -l drwxrwxr-x 2 schkn schkn 4096 Aug 14 20:13 folder lrwxrwxrwx 1 schkn schkn 7 Aug 14 20:38 devconnected -> folder/ $ find . -lname "fold*" ./devconnected
Finding broken links
With the L flag, you can find broken (or daggling) symbolic links on your system.
$ find -L . -type l -ls 258539 0 lrwxrwxrwx 1 schkn schkn 3 Jan 26 2019 ./broken-link -> file
Before finishing this tutorial, there are some quick facts that you need to know about soft and hard links.
- Soft links can point to different filesystems, and to remote filesystems. If you were to use NFS, which stands for Network File System, you would be able to create a symbolic link from one filesystem to a file system accessed by the network. As Linux abstracts different filesystems by using a virtual filesystem, it makes no difference for the kernel to link to a file located on an ext2, ext3, or an ext4 filesystem.
- Hard links cannot be created for directories and they are constrained to the limits of your current filesystem. Creating hard links for directories could create access loops, where you would try to access a directory that points to itself. If you need more explanations about why it can’t be done conceptually, here’s a very good post by Volker Siegel on the subject.
I hope that you learned something new today. If you are looking for more Linux system administration tutorials, make sure to check our dedicated section.
Here is the list of our recent tutorials: