Link and Hard Link. I will just give a brief description on both the types of 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
- 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
# ln -s /path/to/source /path/to/destination
# echo 12345 > ~/myfile.txt
# ln -s ~/myfile.txt /tmp/ # cd /tmp # ls -l lrwxrwxrwx 1 root root 16 May 20 07:26 myfile.txt -> /root/myfile.txt
- 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
- 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 totalSo, we can conclude that all the above mentioned points under soft link are true.
Creating Hard Link
# 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
- 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 totalSo 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 123So 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 +0530So as you see both use the same inode no. due to which any change made to 1 file is reflected on the other file.
Follow the below links for more tutorials:
Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
How to configure Samba 4 Secondary Domain Controller
How to secure Apache web server in Linux using password (.htaccess)
How to register Red Hat Linux with RHN (Red Hat Network )
Red hat Enterprise Linux 5.5 Installation Guide (Screenshots)
15 tips to enhance security of your Linux machine
Why is Linux more secure than windows and any other OS
What is the difference between "su" and "su -" in Linux?
What is kernel-PAE in Linux?
What is swappiness and how do we change its value?
How to log iptables messages in different log file
What are the s and k scripts in the etc rcx.d directories
How to check all the currently running services in Linux
How to auto start service after reboot in Linux
What is virtual memory, paging and swap space?