In the Unix context, what is the difference between a symbolic link and a hard link and which commands are used to create them?
In the Unix context, what is the difference between a symbolic link and a hard link and which commands are used to create them?
A symbolic link (soft link) behaves as a "shortcut" to another file or directory. That is, it practically only points to this other file.
To create a symbolic link, use the -s
option on the ln
command:
# Cria um link simbólico "b" -> "a"
ln -s a b
A hard link is a pointer to the inode of a file or directory.
To create a hard link, use the command ln
:
# Cria um hard link "b" -> "a"
ln a b
I use hardlink for a backup script and the "shortcut" does not have to be in the same directory. Any change in the link changes all other links or source folder to the inode. This happens because the location where the file data is is not the same as the folder we see and click to open and view the data. It's as if the hardlink had a life of its own. Something I do a lot is: cp -al file1 file2. In this case file2 is a hardlink of file1. And I can create multiple hardlink copies that will all work even if I delete the original file1. Search on inode that will understand why it works like this. Linux is amazing.