Well before moving ahead I hope you know about Symlinks in Linux and its both the types i.e Soft Link and Hard Link. I will just give a brief description on both the types of link.
Soft Link
- Using this only a link to the original file is created (shortcut).
- The size of created shortcut is null.
- If you delete the file then the created link (shortcut) won't work.
- In case you delete the shortcut link then it won't affect the original file
Hard Link
- Another copy of the file is created.
- Both the file have same inode no.
- Any changes made in either of the file will appear on the other file.
- Deleting any of the one file won't affect the other file.
Creating Soft Link
The syntax to be followed for creating soft links
# ln -s /path/to/source /path/to/destination
# echo 12345 > ~/myfile.txt
Now we will create a soft link of this file in some other location
# ln -s ~/myfile.txt /tmp/
# cd /tmp
# ls -l
lrwxrwxrwx 1 root root 16 May 20 07:26 myfile.txt -> /root/myfile.txt
Now as you see a symlink has been created which is shown by Blue colour
NOTE: In case if you see a red color symlink instead of blue then it means either the symlink is not created properly or the original file has been moved or deleted.
- Now let us check the difference between both the files.
# stat ~/myfile.txt
File: `myfile.txt'
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 20152421 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-20 07:24:13.000000000 +0530
Modify: 2013-05-20 07:24:12.000000000 +0530
Change: 2013-05-20 07:24:12.000000000 +0530
# stat /tmp/myfile.txt
File: `/tmp/myfile.txt' -> `/root/myfile.txt'
Size: 16 Blocks: 0 IO Block: 4096 symbolic link
Device: fd00h/64768d Inode: 22479090 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-20 07:26:06.000000000 +0530
Modify: 2013-05-20 07:26:03.000000000 +0530
Change: 2013-05-20 07:26:03.000000000 +0530
So both the inode no. seems to be different.
- Let us check the size of both the files
# du -sch ~/myfile.txt 4.0K /root/myfile.txt 4.0K total # du -sch /tmp/myfile.txt 0 /tmp/myfile.txt 0 total
So, we can conclude that all the above mentioned points under soft link are true.
Creating Hard Link
The syntax to be followed for creating soft links
# ln /path/to/source /path/to/destination
# ls -l ~
-rw-r--r-- 2 root root 6 May 20 07:24 myfile.txt
# ln ~/myfile.txt /tmp/
# cd /tmp
# ls -l
-rw-r--r-- 2 root root 6 May 20 07:24 myfile.txt
So as we see there is no difference between both the files and hard to find out which one is the original file and which one is the created hard link.
- Lets check the size of both the file
# du -sch /tmp/myfile.txt 4.0K myfile.txt 4.0K total # du -sch ~/myfile.txt 4.0K /root/myfile.txt 4.0K total
So it seems both the file is occupying the same size on the disk.
- Let us try making some changes in any one of the file
# echo 123 >> ~/myfile.txt # cat /tmp/myfile.txt 12345 123
So the changes made in one file are reflected in another file as you can see above. I appended 1 line in the original file inside root and the same changes are reflected on the other file.
- Let us check the inode number of both the file
# stat ~/myfile.txt
File: `/root/myfile.txt'
Size: 10 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 20152421 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-20 07:50:32.000000000 +0530
Modify: 2013-05-20 07:50:28.000000000 +0530
Change: 2013-05-20 07:50:28.000000000 +0530
# stat /tmp/myfile.txt
File: `/tmp/myfile.txt'
Size: 10 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 20152421 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-20 07:50:32.000000000 +0530
Modify: 2013-05-20 07:50:28.000000000 +0530
Change: 2013-05-20 07:50:28.000000000 +0530
So as you see both use the same inode no. due to which any change made to 1 file is reflected on the other file.
I hope I cleared all your doubt in case still you have any question feel free to comment for any query.