- Hard Link is a mirror copy of the original file.
- Hard links share the same inode.
- Any changes made to the original or Hard linked file will reflect the other.
- Even if you delete any one of the files, nothing will happen to the other hard links.
- But soft link which points to deleted hard link become a dangling soft link.
- You can't link a directory even within the same file system.
- Hard links can't cross file systems.
- Soft Link is a symbolic link to the original file.(more like windows shortcuts)
- Soft Links will have a different Inode value.
- Any changes made to the soft link will reflect the original file and its hard links.
- A soft link points to the original file. If you delete the original file, the soft link fails. It would become dangling symbolic link.
- If you delete the soft link, nothing will happen.
- You can link a directory using soft link on same file system and also on other file system.
- Soft links can cross file systems
Lets learn the difference with an example:
First create a file named with "mainfile.txt"
Now create a hard link named with "hardlink.txt"
From the above output you can find inode number of both files are equal.
Now create a soft link named with "softlink.txt"
Now you find that inode value is different for the soft link and main file.
But equal for the hard link and main file.
Contents of all files before editing
Now lets try to edit main file "mainfile.txt"
From the above output it clarifies that, changes of main file reflects its soft and hard links.
Note: Permission changes of original file reflects only on hard links. Its soft links permission remains unchanged.
Now lets remove main file "mainfile.txt"
So removing the original file will affect the Soft link. The Soft link fails.Now it become a dangling symbolic link(or broken link).Hard link is unaffected.
Now create main file "mainfile.txt" again and make its links as it is before it is
deleted.
let now try to remove soft link "softlink.txt".
It clarifies that deletion of soft link will not affect the main file and its hard links.
Now lets try to edit hard link "hardlink.tst".Before that create soft link "softlink.txt" again.
From the above output its clear that changing the contents of the hard link will reflects on main file and also reflect on soft link of main file.
Now lets try to edit the soft link "softlink.txt".
From the above output its clear that changing the contents of soft link will reflects on main file and also on all links of main file.
Now lets try to remove main file then edit its soft link.
Some strange result.
Soft link creates its main file.But it will not retain main file contents. It merely creates main file "mainfile.txt" with data what you inserted in "softlink.txt".
Now hard link "hardlink.txt" will not have any relation with main file "mainfile.txt".Both files now have unique inode value.
Now lets give a look on directories. How soft links and hard links behaves with directories:
Now create a directory named with temp and create some files in that directory.
Now try to create hard link and soft link for directory temp.
Above example clarifies that it's not possible to create hard link on directory but it is possible to create soft link.
Note:
Like other files, a symbolic link has a separate directory entry with its own inode number. This means that rm can remove a symbolic link even if its points to a directory(or even a device).
So lets try to remove symbolic link of a directory.In our above example "softtemp" is a symbolic link for directory temp.
So it clears that we can remove directory symbolic link with rm command just like a file. But don't add "/" at the end of link(If you use tab to complete file name at command prompt it automatically adds / at the end of link if it points to a directory).
Observations:
- You can link multiple files (i.e., create a link for each), but then the destination filename must be a directory.
Ex: ln chap?? project project is a directory
- When you create hard link, The link count of file increases one. So based on link count you can find how many instances of a file exist in your system.
- So a file is considered to be completely removed from the system when its link count drops to zero
- Many UNIX/Linux commands are linked.
Ex:
[root@localhost ~]# ls -li /usr/bin/gzip
499116 lrwxrwxrwx 1 root root 14 2008-11-11 03:10 /usr/bin/gzip ->../../bin/gzip
- If you want to create symbolic link of a file in other directory your source file path must be absolute.
- You can identify symbolic links by the character "l" seen in the permission field.
- Observe that the size of the symbolic link is equal to length of the path name it contains.
-
To view the permissions of the file or directory that the symbolic link references, use L option with ls -l (i.e. ls -lL )
lrwxrwxrwx 1 root root 14 2008-11-11 03:10 /usr/bin/gzip -> ../../bin/gzip
[root@localhost scripts]# ls -lL /usr/bin/gzip
-rwxr-xr-x 1 root root 64524 2008-02-22 02:55 /usr/bin/gzip