Intro to hard links and symbolic links
What is the difference between a hard link and a symbolic link
Mon Jul 15 2019 - 2 min read
The first thing to do is to define what are hard links and symbolic links. With this knowledge it’s going to be easy to understand their difference and why it is important to know it.
What are hard and symbolic links on linux
A symbolic link, also known as soft link, is a link to the original file, in other words is a special kind of file that points to another file, similar to a shortcut. So it simply points to another entry somewhere in the file system.
A hard link is an additional name for an existing file, in other words hard links refer to the specific location of physical data. It is essentially a label or name assigned to a file.
How to create hard and symbolic links
In order to create a symbolic link with ln command you can type
ln -s source new_soft_link
ls -ali source new_soft_link
To create a hard link with ln command you can type
ln source new_hard_link
ls -ali source new_hard_link
When you use the -i flag it list all files with inode.
Inode is a data structure in a Unix-style file system which describes a filesystem object such as a file or a directory. Each inode stores the attributes nd disk block locations of the object data.
Directories are lists of names assigned to inode.
What is the difference
- A symbolic link does not contain the data in the target file.
- A symbolic link points to another entry somewhere in the file system.
- A symbolic link has the ability to link to directories, or to files on remote computers networked through NFS.
- Deleting a target file for a symbolic link makes that link useless.
- A hard link preserves the contents of the file, it has the actual contents of the original file, so that you can still view the contents, even if the original file is removed or moved.
- A hard link can not be created for directories and they can not cross filesystem boundaries or span across partitions.
- In a hard link you can use any of the hard link names created to execute a program or script in the same manner as the original name given.
These seems to be the most important differences, the best way to understand it is to try all by yourself and see how the inodes are the same or different, depending on the link use it and how they behave if you delete the original file.