This tutorial explains Linux “ln” command, options and its usage with examples.
ln [OPTION]… [-T] TARGET LINK_NAME (1st form)
ln [OPTION]… TARGET (2nd form)
ln [OPTION]… TARGET… DIRECTORY (3rd form)
ln [OPTION]… -t DIRECTORY TARGET… (4th form)
In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link to TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in DIRECTORY. Create hard links by default, symbolic links with –symbolic. When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.
There are two types of links available in Linux :
In computing, a symbolic link (also symlink or soft link) is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution.
With Hard Link, more than one file name reference the same inode number.
When you use link files, it helps us to reduce the disk space by having single copy of the original file.
Here’s the listing of example usage of “in” command:
1. To Create Symbolic Link for file or Directory(ln -s file1 sym_link):
sanfoundry-> touch sample.txt /home/himanshu/ sanfoundry-> cd SAN/ sanfoundry-> ln -s /home/himanshu/sample.txt sym_link.txt sanfoundry-> ls -l sym_link.txt lrwxrwxrwx 1 himanshu himanshu 25 Jul 1 22:49 sym_link.txt -> /home/himanshu/sample.txt
Here as you can see that symbolic link has been created for sample.txt file.
in ls -l output we can see l which means a link is there and it is represented in the output with “->” syntax.
sanfoundry-> mkdir /home/himanshu/sample sanfoundry-> cd SAN/ sanfoundry-> ln -s /home/himanshu/sample/ sym_folder sanfoundry-> ls -l sym_folder lrwxrwxrwx 1 himanshu himanshu 22 Jul 1 22:55 sym_folder -> /home/himanshu/sample/
2. To Create Hard Link for Files(ln file sym_file):
sanfoundry-> touch original_file sanfoundry-> ln original_file sym_file sanfoundry-> ls -i original_file 586082 original_file sanfoundry-> ls -i sym_file 586082 sym_file
Here as we can see that inode number of both the files are same i.e 586082 and it indicates the hard link relation between the files.
3. To Backup the Target Files if it already exists in the list:
sanfoundry-> touch 1.txt 2.txt sanfoundry-> ls 1.txt 2.txt sanfoundry-> ln --backup 1.txt 2.txt sanfoundry-> ls -lrt total 0 -rw-rw-r-- 1 himanshu himanshu 0 Jul 1 23:08 2.txt~ -rw-rw-r-- 2 himanshu himanshu 0 Jul 1 23:08 2.txt -rw-rw-r-- 2 himanshu himanshu 0 Jul 1 23:08 1.txt
Here file 2.txt was already exist so in order to make a link we have take a backup of 2.txt. Here with “–backup” option backup of file 2.txt is created as ‘~2.txt” and link is created as 2.txt.
But if you want to overwrite the existing file and do not want to create a backup use “-f” option.
sanfoundry-> ls 1.txt 2.txt sanfoundry-> ln 1.txt 2.txt ln: failed to create hard link `2.txt': File exists sanfoundry-> ln -f 1.txt 2.txt sanfoundry-> ls 1.txt 2.txt
4. Removing the Original File When a Soft Link is pointing to it:
sanfoundry-> ln -s 1.txt link.txt sanfoundry-> ls -l link.txt lrwxrwxrwx 1 himanshu himanshu 5 Jul 7 01:22 link.txt -> 1.txt sanfoundry-> rm 1.txt sanfoundry-> ls -l link.txt lrwxrwxrwx 1 himanshu himanshu 5 Jul 7 01:22 link.txt -> 1.txt
5. Create Link for Multiple Files at the Same Time:
sanfoundry-> ln -s *.c -t . sanfoundry-> ls -l total 0 -rw-rw-r-- 1 himanshu himanshu 0 Jul 7 01:25 1.txt -rw-rw-r-- 1 himanshu himanshu 0 Jul 7 01:25 2.txt lrwxrwxrwx 1 himanshu himanshu 3 Jul 7 01:27 *.c -> *.c
6. Removing the Hard Linked Files:
sanfoundry-> cat original.txt HI I am a nice boy . I like Cricket. sanfoundry-> ln original.txt link.txt sanfoundry-> rm original.txt sanfoundry-> cat link.txt HI I am a nice boy . I like Cricket.
Here as you can see that if the original file is deleted we can still access the contents of the original.txt through it’s linked file link.txt.
7. Links Help You to Increase the Partition Size Virtually
If you have say two partitions – 10GB and 20GB. The first partition does not have too much free space available in it. If a program located on the first partition needs more space, you can use some of the space from the second partition by creating a link for the files in partion 1.
sanfoundry-> mkdir part2/mnt/logs sanfoundry-> cd part1/logs sanfoundry-> mv * part2/mnt/logs sanfoundry-> rmdir part1/logs sanfoundry-> ln -s part2/mnt/logs part1/logs
Consider that partition1 is mounted on part1/, and partition2 is mounted to part2/mnt/. Let us assume that the logs that are located on partition1 is running out of space, and you’ve decided to move them to partition2.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.