What is the difference between a symbolic link and a hard link?

6

In the Unix context, what is the difference between a symbolic link and a hard link and which commands are used to create them?

    
asked by anonymous 11.06.2015 / 17:53

2 answers

8

A symbolic link (soft link) behaves as a "shortcut" to another file or directory. That is, it practically only points to this other file.

  • When you delete the original file, the symbolic link will fail.
  • Deleting the symbolic link, nothing will happen to the file original.

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.

  • When deleting the original file or the hard link, the other one still exists (because the inode is kept).
  • When you modify any link pointing to the same inode, all links, including the original file, are modified.

To create a hard link, use the command ln :

# Cria um hard link "b" -> "a"
ln a b
    
11.06.2015 / 17:53
-1

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.

    
21.12.2018 / 15:40