Create hard and soft links using the LN command in Linux

Home > Search > Linux commands
  by

The ln command can be used to link one file to another file. There are two types of links, a hard link and a soft link. A hard link is two files that are exact copies of each other. A soft link, also known as a symbolic link or symlink, is a reference from one file to another file.


Hard link

The ln command without any options will create a hard link. Let's take an example where there is a file named file1 in the /home/john.doe directory.

[john.doe@server1 ~]# ls -l
-rw-rw-r--.  1  john.doe  john.doe  183  May 23 18:53  file1

 

The ln command followed by the file that exists and then followed by the new file that will be created will create a hard link. In this example, a hard link is created from /home/john.doe/file1 to /home/jane.doe/file1. File1 cannot exist at /home/jane.doe prior to creating the hard link.

[john.doe@server1 ~]# ln /home/john.doe/file1 /home/jane.doe/file1

 

Every file in Linux has a unique inode number. In this example, the inode of file1 in the /home/john.doe directory is 33554566.

[john.doe@server1 ~]# ls -i /home/john.doe/file1
33554566 file1

 

Likewise, the inode of file1 in the /home/jane.doe directory is also 33554566. This shows that file1 in both the /home/john.doe and /home/jane.doe are the same exact file, and any changes to the file in either directory will change the file in both directories. For example, if john.doe adds "Hello World" to file1, file1 at /home/jane.doe will contain Hello World. Likewise, deleting file1 from either /home/john.doe or /home/jane.doe will delete the file from both directories.

[jane.doe@server1 ~]# ls -i /home/jane.doe/file1
33554566 file1

 


Soft link

Let's take an example where there is a file named file1 in the /home/john.doe directory.

[john.doe@server1 ~]# ls -l
-rw-rw-r--.  1  john.doe  john.doe  183  May 23 18:53  file1

 

The ln command with the -s option will create a soft link. The ln -s command followed by the file that exists and then followed by the new file that will be created will create a soft link. In this example, a soft link is created from /home/john.doe/file1 to /home/jane.doe/file1. File1 cannot exist at /home/jane.doe prior to creating the soft link.

[john.doe@server1 ~]# ln -s /home/john.doe/file1 /home/jane.doe/file1

 

/home/jane.doe/file1 will now show as being linked to /home/john.doe/file1. /home/john.doe/file1 will not show as being linked to /home/jane.doe/file1. 

[john.doe@server1 ~]# ls -l /home/jane.doe/file1
-rw-rw-r--.  1  john.doe  john.doe  183  May 23 18:53  file1 -> /home/john.doe/file1

 

Every file in Linux has a unique inode number. In this example, the inode of file1 in the /home/john.doe directory is 33554566.

[john.doe@server1 ~]# ls -i /home/john.doe/file1
33554566 file1

 

The inode of file1 in the /home/jane.doe directory will not be 33554566. In this example, the inode of file1 in the /home/jane.doe directory is 15422514. This shows file1 in the /home/john.doe and /home/jane.doe are not the same file. Changes made to file1 in the /home/jane.doe directory will change both files. In this example, the phrase Hello World is redirected to file1 in the /home/jane.doe directory.

[john.doe@server1 ~]# echo "Hello World" > /home/john.doe/file1

 

Hello World will appear in both files.

[john.doe@server1 ~]# cat /home/john.doe/file1
Hello World

[john.doe@server1 ~]# cat /home/jane.doe/file1
Hello World

 

Attempting to make a change to file1 in the /home/jane.doe directory will display an error, because this file is a symbolic link.

[john.doe@server1 ~]# echo "Hello There" > /home/jane.doe/file1
-bash: file1: Permission denied

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments