The ln command is used to create a link to a file. Linking enables a file name in one directory (the link) to point to a file in another directory (the target). A link does not contain data of its own, only a reference to the target file. Any changes to the link will reflect in the target file. If you don’t specify the link name, the ln command will create the link in your current working directory.

Syntax

The syntax of the ln command is:

# ln [options] {target name} [link name]

ln Command Options

The ln command has various options. Some of the frequently used options are given in the following table.

Option Used To
–backup Back up existing destination files.
-f Remove existing destination files.
-s Make symbolic links instead of hard links. -i Prompt to remove destination files.
-v Print the name of a file before linking.

Types of Links

Using the ln command, you can create two types of links: hard and symbolic (soft). Hard and symbolic links are a feature of the file system and are common in most file systems supported by Linux. The ext2, ext3, ext4, and XFS file systems all support hard and symbolic links.

Hard Links

A hard link is a reference to another file; it enables the file’s data to have more than one name in different locations in the same file system. Applications treat a hard link as a real file. If the original file is deleted after a hard link is created, all its contents will still be available in the linked file. This is because the inode of a hard link is the same as its target; in other words, it points to the same object on the file system. Hard links cannot be created between two directories, nor can they be created between two files in different file systems.

Soft Links (Symbolic Links)

A symbolic link is a reference to a file or directory that can span multiple file systems. If the original file or directory is deleted after a symbolic link is created, then the original content is lost. This is because the inode of a symbolic link is different than its target; in other words, it points to a different object on the file system. A symbolic link is also known as a soft link.

Examples of links

The following is an example of creating a hard link using the ln command, where / backup/backup-report is the target of the link, and ~/backup-report is the link itself:

$ ln /backup/backup-report ~/backup-report

The following is an example of the same, but creating a symbolic link instead of a hard link:

$ ln -s /backup/backup-report ~/backup-report

ln Command Examples

1. To create a link of an existing file:

2. To create an soft link for the existing file:

3. To make a backup of each destination file:

# ln --backup file.txt file.bk 

4. To allow super user to attempt to create hard linked directories:

5. To create the links with removing the existing destination files:

6. To prompt whether to remove the destination file:

# ln -i
# ln --interactive 

7. To make hard links to symbolic link references:

8. To treat the destination symlink directory as file:

# ln -n
# ln --no-dereference 

9. To make hard links directly to symbolic links:

10. To make symbolic links instead of hard links:

11. To override the usual backup suffix:

# ln -S
# ln --suffix=SUFFIX 

12. To specify the directory to which the directory should be created:

# ln -t
# ln --target-directory=DIRECTORY 

13. To treat the link name as normal file:

# ln -T
# ln --no-target-directory 

14. To print the name of each linked file:

15. To get the help for ln:

16. To get the version info: