Monday, September 16, 2019

Symbolic and Hard Links

Symbolic and Hard links on Linux

What is a hard link?
A hard link is merely an additional name for existing file on Linux and other Unix-like operating systems. Any number of hard links, and thus any number of names can be created for any file. Hard links may also be created for other hard links but they cannot be created for directories and also cannot cross filesystem boundaries.

What is a symbolic link?
A symbolic link is a file that links to another file or directory using its path. In Linux and Unix symbolic links are created with the 'ln' command, and in the Windows command line, symbolic links are created using the 'mklink' command. 

Now what is the difference?
  • Symbolic links , unlike hard links, can link to any file or directory on any computer.
  • In a hardlink you can use any of the hardlink names created to execute a program or script in the same manner as the original name given.
  • A hard link preserves the contents of the file.
  • A soft link does not contain the data in the target file.
Here we use the 'ln' command to make a hard link to the file called file1.
Now we use the command 'ls -i' to view the link.
If you look at the file1 and the hlink1 you can view the inode on the left side is the same. As you can see,  hardlinks act as a shortcut to that file that is hard linked.

Now lets look at Symbolic links.
Using the command 'ln -s' will create a symbolic link between files.
Now we use the 'ls -i' to view the link. 

Notice that only the hardlink file has the same inode as the hardlink while the soft link file has different inodes.

No comments:

Post a Comment