Hardlink Vs Softlink
This is very classic question which you can expect in almost all interviews. Links are basically like creating shortcuts in Windows Operating System. Links will come to rescue you when you want to migrate from one directory to another directory without changing the file location in the actual script.
Hard link generally created by 'ln' command. Syntax is,
$ ln <existing_filename> <hardlink_name>
Example:
$ln fnamex.txt fhardlink.txt
Soft links are also created by same ln command with additionally supplying the options -s . For ex
$ln -s fnamex.txt fsoft.txt
Now run the ls command (see below image) to list out the files and observe the differences. Note: '-i' option for displaying the inode number of the file. To know more about inode refer :
http://en.wikipedia.org/wiki/Inode
You could have observed the following differences:
- All hard links share the same inode number as of original file whereas soft links have different inode
- Soft links can be identified with "l" followed by file permissions
- Hard links have the file size same as Original file. So even after deleting the original file, you can still access with hard link name. Softlinks are useless if the original file deleted. Also note that softlink occupy only few bytes to store file attributes (not actual file content).
- As we created 3 hard links for the same file, count shows 4 s (including the original file count) for hard link. It is not the same for softlinks and it always has "1" as count.
Other major difference that, using hard link you cannot link directories in two different file system. For ex,
But it will work for softlink.
Hope I have covered most of the important differences. Let me know if any questions. Thanks !!
This is very classic question which you can expect in almost all interviews. Links are basically like creating shortcuts in Windows Operating System. Links will come to rescue you when you want to migrate from one directory to another directory without changing the file location in the actual script.
Hard link generally created by 'ln' command. Syntax is,
$ ln <existing_filename> <hardlink_name>
Example:
$ln fnamex.txt fhardlink.txt
Soft links are also created by same ln command with additionally supplying the options -s . For ex
$ln -s fnamex.txt fsoft.txt
Now run the ls command (see below image) to list out the files and observe the differences. Note: '-i' option for displaying the inode number of the file. To know more about inode refer :
http://en.wikipedia.org/wiki/Inode
You could have observed the following differences:
- All hard links share the same inode number as of original file whereas soft links have different inode
- Soft links can be identified with "l" followed by file permissions
- Hard links have the file size same as Original file. So even after deleting the original file, you can still access with hard link name. Softlinks are useless if the original file deleted. Also note that softlink occupy only few bytes to store file attributes (not actual file content).
- As we created 3 hard links for the same file, count shows 4 s (including the original file count) for hard link. It is not the same for softlinks and it always has "1" as count.
Other major difference that, using hard link you cannot link directories in two different file system. For ex,
But it will work for softlink.
Hope I have covered most of the important differences. Let me know if any questions. Thanks !!
No comments:
Post a Comment