Difference between hard link and symbolic link

I thought hard link is alias of a file and symbolic link is another file pointing to the linked file. So, I thought using symbolic link may be safer than using hard link. I thought “rm -f symlink” just delete the symbolic link file itself, leaving the linked file untouched, while “rm -f hardlink” will delete the linked file. It turns out I’m totally wrong.

“rm -f symlink”, “rm symlink”, “rm -f hardlink”, “rm hardlink” all delete the linking file itself, not the linked file, so the target file is safe.

Both “vi symlink” and  “vi hardlink” edit the linked file(target file), not the symlink or hardlink.

Hard link is simpler than symbolic link. “ln targetfile hardlink” just creates an entry in current directory. The new entry contains the file name(“hardlink”) and the inode number of targetfile. Hard links do not create extra inodes, nor do they occupy additional disk space like the data blocks for regular files.( An inode contains all meta-data for a file except file name and actual file content data.)

You can see the directory entry for the hard link is almost the same as the directory entry for the target file(except the file name of course). In fact, you can not tell which is the linked file and which is the linking file after you create the hard link.

Deleting a hard link is just deleting the directory entry. The linked file remains untouched. In fact, deleting the linked file is also just deleting the directory entry, the data blocks for the file are also there un-erased. So, you can access the data blocks via the hard link. Even both the hard link and the target file are removed, the data blocks for the original file are also kept on the disk. But you have no normal method to access the data except using a data-recovery software.

Symbolic link is a little more complicate than hard link. Symbolic link is also also called symlink or soft link. In Windows, it is also called shortcut. You can create a soft link using “ln -s targetfile symlink”.

Like hard link, creating a symbolic link will create an entry in directory file(yes, directory is also a file that contains a fixed structure as its content). But creating a symlink also creates an inode, and possibly creates data blocks to store the target file name. So, creating symlinks takes up a little more disk space than creating hard links, at least, a symlink occupies one more inode than a hard link. Using fast-symlink technique, a short target file name will be stored in the inode structure for the symlink, thus does not need additional disk space for data block. But if the target file name is too long to be saved in inode, it will allocate extra disk space(data block) to save it. You can use “du symlink” to see if data-block is allocated for the symbolic link. “ls -l symlink” or ” stat symlink” command will show the real content size of the symlink, that is, even the target file name is stored in inode, they can report the size of the target file name string.

Other differences between hard link and symbolic link include that hard links cannot cross partition. This is because the inode number is unique for each partition(you can use “df -hi” to show how many inodes are created and how many inodes have been used for each partition), you cannot tell which target  file is being referenced by just reading the inode number from the directory entry for the hard link, if cross-filesystem hard link is allowed).  Soft links do not have this problem because they can store the full file path including the partition name in the file content.

One more difference between hard link and soft link is that you can not create a hard link to directory in modern system. If you try to do so, you will get the error:

ln: ‘/etc/domainhostseotool.com’: hard link not allowed for directory

Another subtle difference between hard link and symlink is after you delete the target file, the symlink will have nothing to point to so it will be displayed red in Linux. Now, “vi symlink” will edit a new file. This is because symlink uses different inode from the target file(you can use “ls -li” to show the inode number for files), and when the target file directory entry is removed, the symbolic link does not know where to find the data block of the target file. Hard link does not have this problem because it keeps the inode number in its own directory entry. Even the directory entry for the target file is removed, a hard link can get the inode from its own directory entry and access the data of the target file.


Posted in tips of hosting