How to Make Links between files

“ln” command

Using Linux “ln” command you can make links between files. There are two types of links i.e Hard Link and Soft Link.

The different between Soft Link and Hard Link as below;

In this article we will see how to create a soft as well as hard link between files.

ln command syntax; 
     ln [OPTION]...  TARGET LINKNAME 
     ln [OPTION]... TARGET 
     ln [OPTION]... TARGET... DIRECTORY 
     ln [OPTION]...  DIRECTORY TARGET...

1) Create a softlink of file and directory.

For demonstration, Let’s create a file named “sample1.txt” and put some content on it, refer below command.

[gaurav@server1 ~]$ echo "Linux is Open Source OS" > sample1.txt
[gaurav@server1 ~]$ cat sample1.txt 
Linux is Open Source OS
[gaurav@server1 ~]$

To create a soft link you can use the “-s” option.

#ln -s <path of original file> <Path of soft link>

[gaurav@server1 ~]$ ln -s sample1.txt  Storage/sample2.txt
[gaurav@server1 ~]$ ls -l Storage/sample2.txt 
lrwxrwxrwx. 1 gaurav gaurav 11 Oct 20 11:07 Storage/sample2.txt -> sample1.txt
[gaurav@server1 ~]$

As above, When We execute the “ls -l” command, the 1st character in first line indicates one of the following file types.The types of Files is shown below;

Note: When you use the soft link on file or directory, the inode number will be different, refer below example.

[gaurav@server1 ~]$ ls -li sample1.txt 
137 -rw-rw-r--. 1 gaurav gaurav 24 Oct 20 11:05 sample1.txt
[gaurav@server1 ~]$ ls -li Storage/sample2.txt 
138 lrwxrwxrwx. 1 gaurav gaurav 11 Oct 20 11:07 Storage/sample2.txt -> sample1.txt
[gaurav@server1 ~]$

Remember: When you want to create the soft link on different file-systems (or across partitions) that are allowed to on different file-systems.

[gaurav@server1 ~]$ ln -s sample1.txt /oracle/sample2.txt
[gaurav@server1 ~]$ ls -li /oracle/sample2.txt 
12 lrwxrwxrwx. 1 gaurav gaurav 11 Oct 20 11:24 /oracle/sample2.txt -> sample1.txt
[gaurav@server1 ~]$

Warning:  When you Link the file on same location with same name, then it will show the error, as refer example.

[gaurav@server1 ~]$ ln -s sample1.txt sample1.txt 
ln: failed to create symbolic link ‘sample1.txt’: File exists
[gaurav@server1 ~]$

2) Create a Hard link of file.

To create a hard link link there is no options used, refer below command.

#ln  <path of original file> <Path of soft link>

[gaurav@server1 ~]$ ln  sample1.txt Storage/sample3.txt 
[gaurav@server1 ~]$

Note: When you use the hard link on file, the inode number will be same, refer below example.

[gaurav@server1 ~]$ ls -li sample1.txt 
137 -rw-rw-r--. 2 gaurav gaurav 24 Oct 20 11:05 sample1.txt
[gaurav@server1 ~]$ ls -li Storage/sample3.txt 
137 -rw-rw-r--. 2 gaurav gaurav 24 Oct 20 11:05 Storage/sample3.txt
[gaurav@server1 ~]$

Remember1 When you want to create Hard Link on different file-systems (or across partitions) that not allowed to on different file systems.

[gaurav@server1 ~]$ ln  sample1.txt /oracle/sample3.txt 
ln: failed to create hard link ‘/oracle/sample3.txt’ => ‘sample1.txt’: Invalid cross-device link
[gaurav@server1 ~]$

Remember2: If original file is deleted then also link will contain data, refer below example.

[gaurav@server1 ~]$ cat sample1.txt
Linux is Open Source OS
[gaurav@server1 ~]$ cat Storage/sample3.txt
Linux is Open Source OS
[gaurav@server1 ~]$ rm -rf sample1.txt
[gaurav@server1 ~]$ cat Storage/sample3.txt
Linux is Open Source OS
[gaurav@server1 ~]$

As above example, When we remove the original file (i.e sample1.txt) the link contain data on link file.

3) Print informative messages.

Using ““-v”” option you can find out what happen during the ln command execution.

#ln  -v <path of original file> <Path of soft link>

[gaurav@server1 ~]$ ln -v  sample1.txt Storage/sample4.txt 
‘Storage/sample4.txt’ => ‘sample1.txt’
[gaurav@server1 ~]$

4) Make a backup when linking.

Using “–backup” option, It will make a backup of the file before making the link by the new one, when target file is already exists.

#ln –backup -s <path of original file> <Path of soft link>

[gaurav@server1 ~]$ ls
  sample1.txt  sample2.txt  Storage
[gaurav@server1 ~]$ ln --backup -s  sample1.txt sample2.txt
[gaurav@server1 ~]$ ls -l
total 8
-rw-rw-r--. 3 gaurav gaurav 24 Oct 20 11:40 sample1.txt
lrwxrwxrwx. 1 gaurav gaurav 11 Oct 20 11:42 sample2.txt -> sample1.txt
-rw-rw-r--. 1 gaurav gaurav 18 Oct 20 11:40 sample2.txt~
drwxrwxr-x. 2 gaurav gaurav 78 Oct 20 11:35 Storage
[gaurav@server1 ~]$

Note: Using “-b” option will create a backup file and that marked by a tilde sign (~) at the end of the file.

5) Create soft link for multiple Files.

Suppose We have two directories (i.e Scripts and Software ). The first directory(i.e Scripts) consist some of the “.sh” files. To create soft links for these files in Second directory (i.e Software), refer below example.

[gaurav@server1 gaurav]# ls
Scripts  Software
[gaurav@server1 gaurav]# ls Scripts/
bash1.sh  bash2.sh  bash3.sh  bash4.sh  bash5.sh
[gaurav@server1 gaurav]# ln -v -s Scripts/*.sh -t Software/
`Software/bash1.sh' -> `Scripts/bash1.sh'
`Software/bash2.sh' -> `Scripts/bash2.sh'
`Software/bash3.sh' -> `Scripts/bash3.sh'
`Software/bash4.sh' -> `Scripts/bash4.sh'
`Software/bash5.sh' -> `Scripts/bash5.sh'
[gaurav@server1 gaurav]# ll Software/
total 0
lrwxrwxrwx. 1 root root 16 Oct 21 14:48 bash1.sh -> Scripts/bash1.sh
lrwxrwxrwx. 1 root root 16 Oct 21 14:48 bash2.sh -> Scripts/bash2.sh
lrwxrwxrwx. 1 root root 16 Oct 21 14:48 bash3.sh -> Scripts/bash3.sh
lrwxrwxrwx. 1 root root 16 Oct 21 14:48 bash4.sh -> Scripts/bash4.sh
lrwxrwxrwx. 1 root root 16 Oct 21 14:48 bash5.sh -> Scripts/bash5.sh
[gaurav@server1 gaurav]#

Note: In above example, We are using “-t” option which stand for target directory that means it will specify the Directory in which to create the links.

Hope this post will help Linux/Unix beginners. Please share you feedback and Comments..!!!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz