ln Command in Linux – Create Symbolic Links (With Examples)

The ln command allows you to create a symbolic link. A symbolic link (symlink) is a special type of file that points to a directory or file.

Before we delve into creating symbolic links, let’s first explore the two types of symbolic links — soft links and hard links.

Soft links point to the location or path of the original file or directory. Soft links are most often used when you want to quickly access a without typing the entire location. It’s also important to know if a symbolic link file is deleted, the original file or directory remains. However, the symbolic link will no longer work if the original file is moved or deleted.

Hard links, in contrast, point to data physically written to disks. They point to an inode or reference file that points to the data’s location. In short, this is much like creating a copy of the file in that you are creating a second filename that refers to the inode data of the original file. Hard links are not considered symbolic links in the purest sense of the word and are not often used by average Linux users.

ln Command Purpose and Syntax

The ln command allows you to create a hard or soft symbolic link. The syntax for the ln command is:

# ln [options] [file/directory] ...  [link name]

Let’s create a symbolic link to a file. In our first example, we will create a link on our desktop, /home/mtravisrose/Desktop/blue-link.txt, to a file in the home directory, /home/mtravisrose/blue.txt.

Open your terminal and enter:

# ln -sv blue.txt /home/mtravisrose/Desktop/blue-link.txt
Create a symbolic link - File Example.
Create a symbolic link – File Example.

Note that blue-link.txt symbolic link was successfully created. The ‘l‘ at the beginning of blue-link.txt – > blue.txt file listing denotes that the file is a symbolic link file.

Let’s create a symbolic link to a file. In our first example, we’re going to create a link on our home directory, /home/mtravisrose/main-ssh-directory, to a directory on our system, /ssh.

Open your terminal and enter:

# ln -s /ssh main-ssh-directory
Create a symbolic link - Directory Example.
Create a symbolic link – Directory Example.

Note that main-ssh-directory symbolic link was successfully created. The ‘l‘ at the beginning of main-ssh-directory – > /ssh directory listing denotes that the file is a symbolic link file.

Creating hard links in Linux is almost identical to creating soft links. In our example, we’re going to create a hard link, red.hard, from a file, red.txt.

Open your terminal and enter:

# ln red.txt red.hard
Create a symbolic link - hard link example.
Create a symbolic link – Hard Link Example.

You can see that the hard link, red.hard, was created from the file, red.txt. Note that the inode is the same for both files.

Soft links can be removed in two separate ways: the rm command and the unlink command.

# rm /home/mtravisrose/Desktop/blue-link.txt
Remove a symbolic link.
Note that the symbolic link, blue-link.txt, was successfully removed, yet the original file, blue.txt, remains.
# unlink main-ssh-directory
Unlink a symbolic link.
Note that the symbolic link, main-ssh-directory, was successfully removed.

It’s important to note that only the symbolic link has been removed with both examples, and the actual file or directory remains.

The rm command does not work on hard links. You must use unlink.

Was this article helpful?

Leave a Comment