Linux ln command with examples

In this article, we will see Linux ln command with examples. This command used to create links between files.

Linux ln command Syntax:

ln [OPTION]... [-T] TARGET LINK_NAME or: ln [OPTION]... TARGET or: ln [OPTION]... TARGET... DIRECTORY or: ln [OPTION]... -t DIRECTORY TARGET...

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.

–>By default, ln command creates hard links.
–>Soft(Symbolic links are create with the option -s or –symbolic

There are two types of links:

1. Soft links or Symbolic links
2. Hard links

1. Soft Links: Refer to a symbolic path indicating the abstract location of another file. It is pointer to another file. Its just like Windows shortcut.

  • Original file and soft link file have different inode, numbers, different file sizes and different timestamps.
  • Usually softlink file has smaller file size than original file size.
  • If we delete original file then softlinks will become useless.

Examples to create soft link with ln command:

ln -s filea.txt fileb.txt

1. Hard Links:Refer to the specific location of physical data. Its just another name of the same exact file.

  • Both original file and hard file have same inode number, same size, same timestamp.
  • If we delete original file, then there is no effect on the hard link file.

Examples to create hard link with ln command:

ln script1.sh scripted.sh

Where script1.sh is original and scripted.sh is the hard link file.

How to remove soft link:

unlink