Search The Site

How To Create Hard Links And Soft Links In Linux

Linux has two different types of links, they are called symbolic links and hard links. Each file in linux is assigned an inode number and a file name. The inode number is a unique number that identifies the file and is assigned by the operating system, and describes the location of the files data on the volume. Symbolic links are different from hard links. Hard links do not normally point to directories, and they cannot link paths on different volumes or file systems. Symbolic links may point to any file or directory irrespective of the volumes on which the source and destination reside. Whereas hard links always refer to an existing file, symbolic links may contain arbitrary text that doesn't point to anything. To delete the file data from the volume you must delete all hard links associated with the file data. A hard link does have some limitations, they cannot be used for linking directories or for linking across different file systems.

To create a hard link called help_backup to a file called help we would enter the following in a terminal window.

ln help help_backup

Soft links or symbolic links are more common. With a soft link the file name is associated with the target file name and not the target files inode. One of the big differences with soft links is that if you delete the target file, the link or links will no longer work, because they are only pointing to the target file name and it is now gone. This also applies if you change the file name of the target file. Soft links can be used for linking directories or for linking across different file systems. If you type ls -F you can see which files are soft links because they end with @

To create a soft link called help_backup to the file help we would enter the following in a terminal window.

ln -s help help_backup