≡ Menu

Symbolic links and hard links in Linux

Linux and all Unix based systems have two types of shortcuts or links that you can create for files. One is the symbolic link which is like a Windows shortcut and the other is a hard link. There are quite a few differences between the two types of links.

As I said, symbolic links are like windows shortcuts. They are soft links and are not really connected to the file system in any way. If you delete a file that has a symbolic link to it, the file still gets deleted and the symbolic link stays as it is. Symbolic links can also link to directories while hard links cannot.

Hard links on the other hand have an inode entry. The inode table contains a counter of how many hard links are there to a file. If you delete a file and there is a hard link to the file, the file will not be deleted, just the counter decremented. Once the counter reaches 0, which means that all hard links to a file have been deleted, the file will be deleted.

How to create a symbolic link?

ln -s /path/to/file1.txt /path/to/link1.txt

The above command will create a link to the file file1.txt and link it to link1.txt.

How to create a hard link?

The command ln without the -s option will create a hard link.

ln /path/to/file1.txt /path/to/link1.txt

The above command will create a hard link to file1.txt.

Note again that hard links cannot link to directories while symbolic links can. Symbolic links can also be links to files outside the system for instance in a mounted file system on a different computer on a network.


Comments on this entry are closed.