Before we go ahead with the difference between Softlink and Hardlink, let’s see what is a link in Linux.
A link in Linux is a pointer to the file, just like shortcuts in windows. They are not actually files but when you open them it will take you to the original file.
Links in Linux behave slightly differently than windows shortcuts. These shortcuts in Linux are called symbolic links or symlinks.
Linux has two types of symlinks, that is Softlink and Hardlink. Let’s see the difference between Softlink and Hardlink.
Softlink
Few things to be noted about Softlink.
- It can link the files and directories located across different file systems.
- The original file and soft linked file don’t share the same inode number.
- Deleting the original folder will break symbolic links.
How to Identify Softlink
When you run the command ls -ltr
you will notice an “l” at the start of the permissions. Just as we saw in Sticky Bit posts where t was appended at the end.
[root@justgeek ]# ls -ltr
lrwxrwxrwx 1 root root 24 Dec 9 08:57 symlink -> /usr/local/orginalfolder
How to create a Softlink
- Creating a test directory to test out Softlink. In this example, I am creating a directory named originalfolder
[root@justgeek ]# mkdir /usr/local/originalfolder
2. Since we have created a folder, now let’s create a symlink to it. We have to mention the folder/file first which currently exists and then the symlink name.
[root@server ]# ln -s /usr/local/originalfolder symlink
The above command created a symbolic link, Let’s run the stat command on both, that is originalfolder and symlink, and see the difference.
You will notice that the inode on both the symlink and originalfolder is different.
[root@justgeek ]# stat symlink
File: ‘symlink’ -> ‘/usr/local/originalfolder’
Size: 24 Blocks: 0 IO Block: 4096 symbolic link
Device: fd01h/64769d Inode: 256014 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-12-09 08:57:22.942621767 +0000
Modify: 2019-12-09 08:57:19.394593754 +0000
Change: 2019-12-09 08:57:19.394593754 +0000
Birth: -
[root@justgeek]# stat /usr/local/originalfolder
File: ‘/usr/local/originalfolder’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd01h/64769d Inode: 395887 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-12-09 08:56:45.194323755 +0000
Modify: 2019-12-09 08:56:44.055314766 +0000
Change: 2019-12-09 08:56:44.055314766 +0000
Birth: -
If you delete the originalfolder, then It will break the symlink.
Few things to be noted about Hardlink.
- You can’t create Hardlink for Directories.
- Unlike Softlink, Hardlink can’t be created across different file systems.
- Hardlink shares the same inode number.
How to create a Hardlink
You will have to use the same ln command to create Hardlink but without the -s part. Since we can’t create Hardlink on the folder we will test it out on file.
- Let’s first create a file that needs to be hard-linked.
[root@justgeek]# touch originalFile.txt
2. Now let’s create a Hardlink to the file originalFile.txt
[root@justgeek ]# ln originalFile.txt hardLink.txt
Simple, Hardlink is created. We will further see the details of both the files so we can learn more about them
We will first cat both the files to make sure that we can same content in both the files.
[root@justgeek]# cat originalFile.txt hardLink.txt
Hey Buddy!!
Hey Buddy!!
When you see the stat of both the files, you will notice that the inode number of both the files are the same.
[root@server ]# stat originalFile.txt hardLink.txt
File: ‘originalFile.txt’
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 256018 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
Birth: -
File: ‘hardLink.txt’
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 256018 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
Birth: -
If you remove the original file it does not affect Hard linked file.
[root@server ]# rm -fv originalFile.txt
removed ‘originalFile.txt’
[root@server ]# ll
total 4
-rw-r--r-- 1 root root 12 Dec 9 09:07 hardLink.txt
[root@server ]# cat hardLink.txt
Hey Buddy!!
[root@server ]#