ln Command In Linux (Create Symbolic Links)

The symbolic link is a connection to the specified file or folder in Linux. Also, the provided symbolic link points to the source file or folder. In this tutorial, we will try to explain the symbolic link with its types like soft link and hard link and how to create the link.

Symbolic Link Types (Hard Link and Soft Link)

There are two types of symbolic links types called hard links and soft links.

  • Soft Link is similar to the shortcuts in the Windows operating system. It is just a pointer to the source. The soft link is more popular than the hard link. Different from the hard link a symbolic link can point to another file system file or folder.
  • Hard Link is similar to the soft link with a big difference. Hard Link uses inodes in order to link to the source file or folder as it exists in the provided location. A hard link can not be created in different file systems and partitions.

ln Command Syntax

The ln command has the following syntax. The default link type for the ln command is a hard link.

  • OPTION is used to set the link type or similar.
  • SOURCE is the source or actual file or folder.
  • LINK is the newly created link for a file or folder.

Create Soft Link For File

The most popular use case for the ln command is creating the soft link. In order to create a soft link, we will use the -s or –symbolic option with the ln command. Then we will provide the source which can be a file and the link. In the following example, we will create a soft link for the file named myfile.txt with the name of mylink.

$ ln -s myfile.txt mylink

The absolute full path can be used to create a soft link. In the following example, the file is located under the user’s home directory and we will create the link in the /var directory.

$ ln -s /home/ismail/myfile.txt /var/mylink

Show Link Source

Links are popularly used with the Linux distributions in order to provide different files and folders in different paths without copying and pasting. The link sources can be displayed in the command-line interface and desktop environment by using a file manager. For desktop environments, the file managers use different icons to depict a link to a file.

In terminal “ls -l” command can be used to list files and their types where the links will be listed and their source also provided with the -> sing. Also, the file type contains the l to describe it as a link type at the start of the permissions.

Show Link Files with ls Command
lrwxrwxrwx 1 ismail ismail       10 Ara 26 17:05 mylink -> myfile.txt

Create a Soft Link For the Folder or Directory

Not only files are used to create soft links a file or directory link can be created by using the ln command. As stated previously the link type will be set as a soft link with the -s or –symbolic parameters. We will create the soft link for the directory named /var/lib inside the user home directory.

$ ln -s /var/lib /home/ismail/lib

Overwrite Existing Symbolic Link

In some cases, a file or symbolic can exist already and we may try to create the same name link. We will get the following error.

ln: failed to create symbolic link 'mylink': File exists

We should overwrite the existing link or file to create a new symbolic link. This requires overwriting the existing link. The -f option is used to overwrite the existing link.

$ ln -f -s myfile.txt mylink

Create Hard Link For File and Folder

A hard link is different from a soft link where the real file inodes are copied into the link. If you rm this link the original file will be deleted too. Hard link creation does not require a parameter for the ln command.

$ ln myfile.txt mylink

We can also create a hard link with the full or absolute path like below.

$ ln /home/ismail/myfile.txt /var/myfile

Remove or Delete Symbolic Link

The links can be deleted by using the unlink command or rm command. The native command to remove or delete a symbolic link is the unlink command. Just provide the link name or path to remove it. The unlink command can delete both soft links and hard links also with an absolute or relational path. The unlink command syntax is like below which is very simple. Only the link we want to remove/delete is provided as a parameter to the unlink command.


In the following example, we delete the link named mylink using the unlink command.

$ unlink mylink

Find and Delete Broken Symbolic Links

During time symbolic links are created, used, removed even broken. A symbolic link is broken when the destination file or directory is removed or changed. Broken links can be easily found or deleted using the find command.

$ find /home/ibaydan -xtype l -delete

Leave a Comment