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

Asked

Viewed 12,917 times

6

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

  • If you want to delve into how Linux manages the filesystem, see this article: https://docstore.mik.ua/orelly/networking/puis/ch05_01.htm#PUIS-CHP-5-SECT-1

2 answers

11


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

  • When deleting the original file, the symbolic link will fail.
  • When deleting the symbolic link, nothing will happen to the file original.

To create a symbolic link, use the option -s in charge ln:

# 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 hard link, the other one still exists (because the inode is kept).
  • When modifying 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
  • Hard link needs to be on the same partition as each other? I know soft link does not, but hard link begets me questions

-1

I use hardlink for a backup script and the "shortcut" does not need to be in the same directory. Any change in the link changes all other links or source folder to inode. This happens because the location where the file data is not the same as the folder we see and click to open and view the data. It’s like hardlink has a life of its own. Something I do a lot is: cp -al file1 file2. In this case file2 is a file1 hardlink. And I can create multiple hardlink copies that will all work even if I delete the original file1. Research on inode that will understand why it works like this. Linux is amazing.

  • 1

    Hi. You talked about cp -al which creates a hard link but did not explain about the difference between the symbolic link and a hard link.

  • Soft Link is a shortcut that points to a file and this file points to the inode that is the actual location on the disk. Hard Link points directly to the actual file inode on the disk. If you have several Hard Link it would be as if you had several copies of the file and all point to the inode of the file itself. In soft Link if you delete the file it points to, you will not have access to the file and the shortcut will stop working. On the Hard Link even if you delete the file the Hard Link will continue working because it is connected directly to inode which is the real location on the disk. Then it is safer.

  • Hello! When working out your answer better, you need to edit it. Don’t use the comment space, edit your answer. Greetings.

  • Add this comment to your reply content.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.