Linux Hard Links vs Symbolic Links and How to Create Them
The ln command is used to create links on a Linux filesystem. There are two different kinds of links, hard and soft (symbolic). Here we will discuss what both are, and how to use them. Then we will discuss some facts about each.
Hard links are a way of associating a multiple file names with an inode (index node). Inodes are objects that store attributes and block locations (metadata) of a file or directory. Each file on a single filesystem has a unique inode associated with it. Since hard links use inodes that means each link and file use the same data blocks on the hard disk. This limits them to being stored in the same filesystem, because inodes are filesystem specific. Think of a hard link as two names for the same exact file. Any changes you make to one effects both (except name changes). If you change the permissions of a hard link, it changes the permissions of the file. Remember, they share the same attributes written in the inode.
To create a hard link, we use the ln command with the following syntax.
ln /path/to/target /path/to/link
Here we will link two files, then we will list out each file and compare their inodes.
$ ln /home/savona/Downloads/TinyCore-current.iso /home/savona/Desktop/SmallLinuxDistro.iso
Now that we created the link, let's use the ls command with the -i (inode) option to inspect their inode numbers.
$ ls -li /home/savona/Downloads/TinyCore-current.iso 416642 -rw-rw-r--. 2 savona savona 18874368 Mar 19 2018 /home/savona/Downloads/TinyCore-current.iso $ ls -li /home/savona/Desktop/SmallLinuxDistro.iso 416642 -rw-rw-r--. 2 savona savona 18874368 Mar 19 2018 /home/savona/Desktop/SmallLinuxDistro.iso
As you can see, they share the same inode (in bold).
Soft Link (Symbolic / Symlink)
Soft / Symbolic links are basically a pointer to another file (Like a Windows shortcut for Linux). The soft link just holds the path of the target file instead of the block locations on the disk. Since they are not tied to inodes, symlinks can span filesystems.
To create a soft /symbolic link, we use the ln command with the -s option.
ln -s /path/to/target /path/to/link
Let's create a symlink and see the differences.
$ ln -s /home/savona/Downloads/TinyCore-current.iso /home/savona/Desktop/Synlink-to-iso
Now we will list them out and you will see that the symlink does NOT contain an inode. You will also notice that the first column of the permissions is an l denoting it is a link while on the hard link it does not.
$ ls -li /home/savona/Downloads/TinyCore-current.iso 416642 -rwxrwxrwx. 2 savona savona 18874368 Mar 19 2018 /home/savona/Downloads/TinyCore-current.iso ls -li /home/savona/Desktop/Synlink-to-iso 416555 lrwxrwxrwx. 1 savona savona 43 Jan 21 01:03 /home/savona/Desktop/Synlink-to-iso -> /home/savona/Downloads/TinyCore-current.iso
Hard Links vs Soft (Symbolic) Links
Here are some facts about hard vs soft links.
- Hard links can ONLY be used on the same filesystem.
- Symbolic links can span across filesystems.
- Hard links stay linked, even if you move the link or the target.
- Symbolic links will ALWAYS break if you move the target (original) file, because the link is using the path on the file, which will change when moved.
- A Symbolic link will sometimes break if you move the link, depending on how it was created (absolute vs relative path).
- When you change the attribute (permissions, owners, modification time, etc) of a hard link, it changes the targets as well.
- If you delete a hard link, it DOES NOT delete the file. This is because the inode has a reference counter that increases with each hard link. The file will only be deleted if the counter reaches 0.
- A symlink can be deleted with no effect to the target file.
- You can have create multiple symlinks or hard links.
- A symlink can be made for ANY file or directory (with minor excepts).
- You can NOT hard link to a directory.
I hope this came across as clearly as it sounds to me. If you have any questions or information to add just sound off in the comments.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Join Our Newsletter
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (60)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
Leave a Reply