The “ln” Command in Linux [6 Practical Examples]

The ln command in Linux is a very user-friendly command used to create links (shortcuts) to the source file/directory. The benefit of using links is more than one file refers to the same original file. In this article, I will demonstrate to you the syntax, options & practical applications of ln command in Linux.

Types of “ln” Command

The ln command in Linux creates links (shortcuts) to the files/directories. There are two types of links and these are:

1. Hard Link: When a file is linked with a hard link, if you make any changes to any of the files this will reflect on both files. No option is needed to create a hard link to the file.

2. Symbolic Link: When a file is linked with a symbolic link if any of the files/directories are moved or deleted,  this will break the link between the files. The option -s is required to create a symbolic link to the file. The symbolic link is also known as the soft link.

Syntax of “ln” Command

The ln command in Linux has a simple syntax. It includes the ln command, options & name of the source file & name of the linked file. The syntax is as follows:

ln [OPTION]... [SOURCE_FILE] [LINKED_FILE]
EXPLANATION

In the syntax above, OPTION is enclosed by a square bracket and followed by 3 dots representing that more than one option can be used at the same time. Moreover, SOURCE_FILE represents the name of the source file, and LINKED_FILE denotes the name of the linked file created with the ln command.

Options of “ln” Command

A few options are available for the ln command in Linux. I have listed here some of the most useful options for this command. However, you can look for the ln command on the man page to see more options for this command.

man ln

Here are some useful options for ln Command:

Options Description
-s Creates symbolic links to files/directories.
-b Creates a backup of the file.
–version Displays version information & exits.
–help Displays help page & exits.

Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be alert while using them.

Practical Examples of the “ln” Command in Linux

The ln command in Linux is a user-friendly command used to create links to source files/directories. Here you will learn some useful examples of the ln command in Linux. In this article, I have worked with the following files.

1. Using “ln” Command to Create a Hard Link to a File in Linux

To create a hard link to a file, you can use the ln command in Linux. The file I am using here to be linked is file1.txt. To do the same follow the steps below:

  1. First launch the Ubuntu Terminal. Then to view the files, write ls command in the command prompt.
  2. Type the following command in the command prompt to create a hard link to a file1.txt:
    ln file1.txt hardlink.txt.
  3. To view the hard link file, write ls command again in the command prompt.
  4. To view the contents inside the file1.txt, copy the following command in the command prompt and then hit the ENTER button.
    cat file1.txt
  5. Similarly, to view the contents inside the hardlink.txt, type the following command in the command prompt and then strike the ENTER button.
    cat hardlink.txt

In the image below, you can see the hard link to file1.txt is created using the ln command in Linux.

Create a Hard Link to a File Using the “ln” Command in Linux

Note: Any changes made to either “file1.txt” or “hardlink.txt” will be reflected in both text files.


Similar Readings


2. Using “ln” Command to Create a Symbolic (Soft) Link to a File in Linux

To create a symbolic soft link to a file, I am using the ln command with the -s option in Linux. Here, I want to create a link to file2.txt. To do the same follow the steps:

  1. After opening the terminal, type the following command in the command prompt to create the symbolic link to a file2.txt:
    ln -s file2.txt symlink.txt.
  2. To display the soft link file, you can type ls command in the command prompt.
  3. To view the contents inside the file2.txt, type cat file2.txt in the command prompt.
  4. To view the contents inside the symlink.txt, type cat symlink.txt command in the command prompt and then strike the ENTER button.

In this image, you can see the soft link to file2.txt is created using the ln command in Linux.

Create a Symbolic(Soft) Link to a File Using the “ln” Command in Linux

In the image below, you can see how the file of the symbolic link looks.

Create a Symbolic(Soft) Link to a File

Note: If file2.txt is deleted or moved somewhere, the link will become a broken link.

3. Using “ln” Command to Create a Symbolic (Soft) Link to a Directory in Linux

To create a symbolic link to a directory, I am using the ln command with the -s option in Linux. Here I am showing how to create a link to the newfolder:

  1. Enter the following command in the command prompt after opening your terminal to create a symbolic link to a directory:
    ln -s newFolder/ SymLink
  2. To view whether the symbolic link of the directory is created or not, write ls command in the command prompt and then hit the ENTER button.
  3. To view the contents of newFolder, type ls newFolder/ command in the command prompt.
  4. To view the contents of SymLink, apply ls SymLink/ command in the command prompt.

In the image below, you can see the symbolic link to file2.txt is created using the ln command in Linux and SymLink contains the same contents as newFolder.

Create a Symbolic(Soft) Link to a Directory Using the “ln” Command in Linux

In the following image, you can see how the directory of the symbolic link looks.

Create a Symbolic(Soft) Link to a Directory Using the “ln” Command in Linux

Note: If the newFolder is deleted or moved somewhere, the link will become a broken link.

4. Create a Hard Link to a File With the Absolute Path

To create a hard link to a file with the absolute path, you can use the ln command in Linux. Here, I am creating a link using the absolute path of the source file named file1.txt & linked file named hardlink.txt:

  1. Copy the following command in the command prompt to create a hardlink:
    ln /home/sylvie/MyFolder/file1.txt /home/sylvie/MyFolder/hardlink1.txt
  2. To view the hard link file, write ls MyFolder/ command in the command prompt.

As you can see in the image below, the hard link to file1.txt is created with the absolute path using the ln command in Linux.

Create a Hard Link to a File With the Absolute Path


Similar Readings


5. Unlink Symbolic Link

To unlink a symbolic link, I am using the unlink command in Linux. Follow the procedure:

  1. Type the following command in the command prompt to unlink a symbolic link:
    unlink symlink.txt
  2. To view whether the file is still linked or not, write ls in the command prompt.

As you can see in the following image, the symbolic link to file2.txt is unlinked using the unlink command in Linux.Unlink Symbolic Link

6. Using the “ln” Command to View Version in Linux

To view the version, I am using the ln command and option --version in Linux. Now to accomplish this task, write the following command in the command prompt:

ln --version

In the image, it is showing the version using the ln command in Linux.

View the Version

Conclusion

In this article, you got to know that the ln command has significant uses in Linux. You’ve also found the syntax, some useful options, and the practical application of this command. So, learn this command and execute it accordingly to become an expert user.

People Also Ask

Why use ln in Linux?

Using the ln command, you can create a symbolic link or a hard link to a file or directory. A hard link points to a file’s inode, which stores data on disk. This makes it possible to use multiple filenames for the same file.

How to undo ln Linux?

To undo ln command in Linux, you can use rm command. This command is also used for deleting files and directories. The basic syntax to undo a symbolic link is: rm link_name. You can also undo hard links with this rm command. Just replace the link_name with the hard_link_name.

What does sudo ln do?

The sudo ln command is utilized to establish links between files with superuser privileges. The ln command itself is used for the creation of links. In contrast, the sudo command allows a user to execute a command as a superuser.

What is the difference between ln and ln s in Linux?

The ln command creates hard links whereas the ln -s command creates soft links.

What is unlink command in Linux?

The unlink command is used to remove files and links in Linux. For instance, to remove the file.txt, you can use this syntax: unlink file.txt.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment