Soft and Hard links in Unix/Linux

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

--

Links in UNIX could be taken as pointers to a file or directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file, elsewhere.

There are two types of links :

  1. Soft Link or Symbolic links
  2. Hard Links

A symbolic or soft link is an actual link to the original file, whereas a hard link is a mirror copy of the original file. If you delete the original file, the soft link has no value, because it points to a non-existent file. But in the case of hard link, it is entirely opposite. Even if you delete the original file, the hard link will still has the data of the original file. Because hard link acts as a mirror copy of the original file.

Both behave differently if you delete or move them. Symbolic links are not updated, they simply store a name given to the destination path.

Hard link

  • can’t cross the file system boundaries (i.e. A hardlink can only work on the same filesystem),
  • can’t link directories,
  • has the same inode number and permissions of original file,
  • permissions will be updated if we change the permissions of source file,
  • has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.

Soft link

  • can cross the file system,
  • allows you to link between directories,
  • has different inode number and file permissions than original file,
  • permissions will not be updated,
  • has only the path of the original file, not the contents.

Syntax symbolic link

ln -s [original file name] [link name]

test — creating a symbolic link

As you see in the above output, softlink.txt displays the same data as file.txt Let us check the inodes and permissions of softlink.txt and file.txt

With the command [ls -lia]

The first column is the inode, the second column the permissions.

As we see in the above screenshot, the inode number (11665731 vs 11665692) and file permissions (lrwxrwxrwx vs -rw-rw-r — ) are different, even though the softlink.file has same contents as file.txt .Hence, it is proved that soft link don’t share the same inode number and permissions of original file.

Now, remove the original file (i.e file.txt) and see what happens.

As you see above, there is no such file or directory called softlink.txt after we removed the original file (i.e file.txt). So, now we understand that soft link is just a link that points to the original file. The softlink is like a shortcut to a file. If you remove the file, the shortcut is useless.

  • As you already know, if you remove the soft link, the original file will still be available.
link “dangling”, i.e. they have no value.
  • If you delete the original file, the symbolic link becomes a “dangling” link pointing to a non-existent file.

Syntax hard link

ln [original file name] [link name]

As you see in the above output, hardlink.txt displays the same data as file.txt Let us check the inodes and permissions of hardlink.txt and file.txt

With the command [ls -lia]

Now, we see that both hardlink.txt and file.txt have the same the inodes number (11665692) and file permissions (-rw-rw-r — ). Hence, it is proved that hard link file shares the same inodes number and permissions of original file.

Note: If we change the permissions on source.txt, the same permission will be applied to the hardlink.txt as well.

Now, remove the original file (i.e source.file) and see what happens.

As you see above, even if I deleted the source file, I can view contents of the hardlink.file. Hence, it is proved that Hard link shares the same inode number, the permissions and data of the original file.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Paola Carrero
Paola Carrero

Written by Paola Carrero

0 Followers

Software Engineer in progress

No responses yet

Write a response