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.
Types of Symbolic Links
Before we delve into creating symbolic links, let’s first explore the two types of symbolic links — soft links and hard links.
Soft 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
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]
Create a Symbolic Link to a File
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
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.
Create a Symbolic Link to a Directory
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
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.
Create a Hard Link
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
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.
Remove or ‘Unlink’ a Symbolic Link
Soft links can be removed in two separate ways: the rm command and the unlink command.
Remove a Symbolic Link with rm
# rm /home/mtravisrose/Desktop/blue-link.txt
Remove a Symbolic Link with unlink
# unlink main-ssh-directory
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.