ln Command in Linux with Examples

The ln command in Linux is used to create links between files or directories. It is utilized to save disk space, maintain consistency between files, and improve organization.

Types of Links
There are two types of links that can be created using ln: Hard Links and Symbolic (Soft) Links.

  • Hard links: Hard links in Linux create additional pointers to the same data without duplicating the file. Both the original and linked files share the same unique identifier (inode number) on disk. Deleting one linked file won’t affect the other; it remains intact.
  • Symbolic (Soft) links: A symbolic link (or symlink) is a special file that points to another file by its pathname. It acts as a pointer to the target file. If the original file is deleted, the symlink becomes a “dangling symlink”.

Creating Links

The basic syntax for creating a link with ln is:

ln [options] target linkname
  • options are optional flags that specify additional behavior for the ln command.
  • target is the name of the file or directory to which you want to create a link.
  • linkname is the name of the link that will be created.

Creating Hard Links

By default, ln creates hard links. To create a hard link to a file, use the following syntax:

advertisement
advertisement
ln file1 file2

This will create a link named file2 that points to the same data on disk as file1.

Creating Symbolic Links

To create a symbolic link to a file, use the -s option:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
ln -s file1 file2

This will create a symbolic link named file2 that points to file1.

ln Command Examples:

Here’s the listing of example usage of “in” command:

Example 1: Creating Symbolic Links for File and Directory

advertisement

For File

touch sample.txt /home/himanshu/
cd SAN/
ln -s /home/himanshu/sample.txt sym_link.txt
ls -l sym_link.txt

Creates a symbolic link named sym_link.txt pointing to sample.txt file. The ls -l command displays the link created with the arrow symbol (->) denoting the linked path.

Output:

lrwxrwxrwx 1 himanshu himanshu 25 Jul  1 22:49 sym_link.txt -> /home/himanshu/sample.txt

For Directory

advertisement
mkdir /home/himanshu/sample
cd SAN/
ln -s /home/himanshu/sample/ sym_folder
ls -l sym_folder

It creates a symbolic link named sym_folder pointing to /home/himanshu/sample/ directory.

Output:

lrwxrwxrwx 1 himanshu himanshu 22 Jul  1 22:55 sym_folder -> /home/himanshu/sample/

Example 2: Creating Hard Links for Files

touch original_file
ln original_file sym_file
ls -i original_file 
586082 original_file
ls -i sym_file 
586082 sym_file

It generates a hard link named sym_file pointing to original_file. The ls -i command displays the inode numbers, which being the same (586082) indicate the hard link relation between the files.

Example 3: Creating Link with Backup and Overwriting Existing File

# Create files 1.txt and 2.txt
touch 1.txt 2.txt 
ls
# Output: 1.txt  2.txt
 
# Create a link with backup for 1.txt as 2.txt already exists
ln --backup 1.txt 2.txt 
ls -lrt
# Output:
-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.

# Attempt to create a link without overwriting the existing file
ln 1.txt 2.txt 
# Output: ln: failed to create hard link `2.txt': File exists
 
# Overwrite the existing file using -f option
ln -f 1.txt 2.txt 
ls 
# Output: 
1.txt  2.txt

Example 4: Removing Original File When Soft Link Points to It

ln -s 1.txt link.txt 
ls -l link.txt
lrwxrwxrwx 1 himanshu himanshu 5 Jul  7 01:22 link.txt -> 1.txt
rm 1.txt 
ls -l link.txt 
lrwxrwxrwx 1 himanshu himanshu 5 Jul  7 01:22 link.txt -> 1.txt

Creates a soft link link.txt pointing to 1.txt. After removing 1.txt, link.txt remains but now points to a non-existing file.

Example 5: Creating Links for Multiple Files at Once

ln -s *.c -t . 
ls -l

It creates a symbolic links for all .c files in the current directory (-t .) to the current directory. However, the output displays *.c as symbolic links due to no actual .c files in the directory.

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

Example 6: Removing the Hard Linked Files:

# Displaying content of original.txt
cat original.txt 
# Output:
HI I am a nice boy. 
I like Cricket.
 
# Creating a hard link from original.txt to link.txt
ln original.txt link.txt 
 
# Deleting the original file original.txt
rm original.txt 
 
# Displaying content of link.txt after deleting original.txt
cat link.txt 
# Output:
HI I am a nice boy . 
I like Cricket.

The file ‘original.txt’ is created and displayed using ‘cat’. A hard link ‘link.txt’ is created pointing to ‘original.txt’. Upon deleting ‘original.txt’ using ‘rm’, the content remains accessible through ‘link.txt’. Hard links retain the file content until all links pointing to it are deleted.

Example 7: Links Help You to Increase the Partition Size Virtually

# Creating directories for part2/mnt/logs
mkdir part2/mnt/logs
 
# Navigating to part1/logs
cd part1/logs
 
# Moving files from part1/logs to part2/mnt/logs
mv * part2/mnt/logs
 
# Removing part1/logs
rmdir part1/logs
 
# Creating a symbolic link to part2/mnt/logs from part1/logs
ln -s part2/mnt/logs part1/logs

If ‘partition1’ (mounted on part1/) faces space constraints and ‘partition2’ (mounted on part2/mnt/) has sufficient space, the ‘mv’ command moves files from ‘partition1’ to ‘partition2’. Then, ‘part1/logs’ is removed and replaced by a symbolic link ‘part2/mnt/logs’ using ‘ln -s’. This enables accessing the logs originally located on ‘partition1’ from ‘partition2’ virtually through the symbolic link.

Additional Options

The ln command has a number of additional options that can be used to control its behavior. Some of the most common options are:

  • -f: Force the creation of links, even if they already exist.
  • -b: Create backup files of the target files before creating links.
  • -v: Display verbose output, showing which files are being linked.
  • -h: Display help information.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.