HARDLINK & SOFTLINK command in Linux with Examples
HARDLINK & SOFTLINK command
There are two types of links available in Linux -Hardlink & Softlink. Linux in command is used to create either soft or hard links.
Hard Link
Hard Link is a mirror copy of the original file. Hard Link shares the same inode number. Any changes made to the original or Hard Linke file will reflect the other.
Even if you delete any one of the files, nothing will happen to the other. Hard Links can' t
Cross file systems.
Soft Link
Soft Link is a symbolic Link to the original file. Soft Link will have a different inode numbers. Soft Link points to the original file. If you delete the original file, the Soft Link will fail. If you delete the Soft Link file, nothing will happen.
Hard Link | Soft Link |
Hard link have same inode number | Soft Link have different inode numbers |
Link have actual file contents | Soft Link contains the path for original file and not the contents |
Cannot create Hard Link for directory | Soft Link can create Link for directory |
Removing any Link it doesn' t affect other Links | The original file removing it affects the Link file |
Cannot identify the Link file | Can identify the Link file |
To create a Soft Link for file
The following example creates a symbolic Link for the file.
Example
[root@localhost Public]# ls -lvi total 4 7870932 -rw-r--r-- 1 root root 0 Feb 27 16:20 file1.txt [root@localhost Public]# ln -s /root/Public/file1.txt /root/Pictures/file1link.txt [root@localhost Public]# ls -lvi /root/Pictures/file1link.txt 7870934 lrwxrwxrwx 1 root root 22 Feb 27 16:24 /root/Pictures/file1link.txt -> /root/Public/file1.txt
To create soft link for directory
If you want to create soft link for the directory you can follow the below command.
Example
[root@localhost Public]# ls -lvi 7870935 drwxr-xr-x 2 root root 4096 Feb 27 16:31 softlink [root@localhost Public]# ln -s /root/Public/softlink/ /root/Pictures/softlink1 [root@localhost Public]# ls -lvi /root/Pictures/softlink1 7870936 lrwxrwxrwx 1 root root 22 Feb 27 16:32 /root/Pictures/softlink1 -> /root/Public/softlink/
To create a hard link for the file2link.txt
The following example creates a Hard Link for the file.
Example
[root@localhost Public]# ls -lvi 7870933 -rw-r--r-- 1 root root 0 Feb 27 16:20 file2link.txt [root@localhost Public]# ln /root/Public/file2link.txt /root/Pictures/filehard.txt [root@localhost Public]# ls -lvi /root/Pictures/filehard.txt 7870933 -rw-r--r-- 2 root root 0 Feb 27 16:20 /root/Pictures/filehard.txt
Create Link Across Different Partitions
When you want to create the link across Partitions, You are allowed to create the symbolic link only, Create the Hard Link across the Partitions not allowed.
Example
[root@localhost ]# mount /dev/sda5 /mnt
[root@localhost ]# cd /mnt
[root@localhost ]# ls
[root@localhost ]# main.c Makefile
[root@localhost ]#ln Makefile /tmp/Makefile
ln: creating hard link `/tmp/Makefile' to `Makefile' : Invalid cross-device link
Create the Target Files If Already Exists
When you create a new link (If another file exist already with the same name as the new link name), you instruct in command to take a backup of the original file before creating the new link using the &ndash backup option as shown below.
Example
[root@localhost Public]# ls -lvi total 4 7870932 -rw-r--r-- 1 root root 0 Feb 27 16:20 file1.txt [root@localhost Public]# ln --backup -s file1.txt file2.txt [root@localhost Public]# ls -lvi total 4 7870932 -rw-r--r-- 1 root root 0 Feb 27 16:20 file1.txt 7870954 lrwxrwxrwx 1 root root 9 Feb 27 17:13 file2.txt~ -> file1.txt
Create Soft Link Using “ No-Deference” in Command Option
While creating a new Soft Link, normally OS would de-reference the destination path before it creates the new soft link.
Example
[root@localhost Public]# ls -lvi 7870956 drwxr-xr-x 2 root root 4096 Feb 27 17:25 test [root@localhost Public]# ln -s /etc/passwd test [root@localhost Public]# cd test/ [root@localhost test]# ll total 0 lrwxrwxrwx 1 root root 11 Feb 27 17:25 passwd -> /etc/passwd
Create Link for multiple directory at the same time
In the following example, there are two directories dir1 and dir2..If you want to create soft links for these files in dir2, you’ ll typically do it one by one. Instead, you can create soft link for multiple files together using -t option as shown below.
must to be finish with dot the end
Example
[root@localhost Public]# ls -l total 16 drwxr-xr-x 2 root root 4096 Feb 27 17:31 dir1 drwxr-xr-x 2 root root 4096 Feb 27 17:31 dir2 [root@localhost dir2]# ln -s ../dir1/* .tct -t . [root@localhost dir2]# ls -l total 0 lrwxrwxrwx 1 root root 17 Feb 27 17:34 file1.txt -> ../dir1/file1.txt lrwxrwxrwx 1 root root 17 Feb 27 17:34 file2.txt -> ../dir1/file2.txt
Removing the Original File When a Soft Link is pointing to it
When the original file referred by a soft-link is deleted, the soft link will be broken as shown below.
Example
[root@localhost dir1]# ls -l total 0 -rw-r--r-- 1 root root 0 Feb 27 17:32 file1.txt -rw-r--r-- 1 root root 0 Feb 27 17:32 file2.txt [root@localhost dir1]# ls -l ../dir2/file1.txt lrwxrwxrwx 1 root root 17 Feb 27 17:34 ../dir2/file1.txt -> ../dir1/file1.txt [root@localhost dir1]# rm file1.txt [root@localhost dir1]# ls -l total 0 -rw-r--r-- 1 root root 0 Feb 27 17:32 file2.txt [root@localhost dir1]# ls -l ../dir2/file1.txt lrwxrwxrwx 1 root root 17 Feb 27 17:34 ../dir2/file1.txt
Links Help You to Increase the Partition Size Virtually
Let us assume that you have two partitions &ndash 20GB and 40GB. The first partition does not have too much free space available in it. If a program located on the first partition needs more space. Space from the second partition by creating a link for the log files as shown below.
Consider that partition1 is mounted on /, and partition3 is mounted to /mnt/. Let us assume that the logs that are located on partition1 are running out of space, and you’ ve decided to move them to partition2. You can achieve this as shown below.
Example
[root@localhost ]#mkdir /mnt/logs
[root@localhost ]#cd /logs
[root@localhost ]#mv * /mnt/logs
[root@localhost ]#cd / rmdir logs
[root@localhost ]#ln -s /mnt/logs logs
Remove the Hard Link files
When you delete a file that is hard linked, you would be still able to access the content of the file until you have the last file which is hard linked to it.
Example
[root@localhost Public]# ls &ndash lvi 7870933 -rw-r--r-- 1 root root 0 Feb 27 16:20 file2.txt [root@localhost Public]# ln /root/Public/file2.txt /root/Pictures/filehard.txt [root@localhost Public]# ls -lvi /root/Pictures/filehard.txt 7870933 -rw-r--r-- 2 root root 0 Feb 27 16:20 /root/Pictures/filehard.txt Delete the original file [root@localhost Public]#rm file2.txt
You can still now access the original file by using the hard link you created.
Comments ( 0 )
No comments available