How to Create Symbolic link in Linux Using Ln Command

A symbolic link is commonly referred to as soft link or symlink, which is a special type of file that references another file or directory.

In this guide, you will learn how to create a symbolic link in Linux using ln command to make links between files.

Links Types

There are two types of links in Linux/UNIX systems:

  • Hard links. You can think a hard link as an additional name for an existing file. Hard links are associating two or more file names with the same inode . You can create one or more hard links for a single file. Hard links cannot be created for directories and files on a different filesystem or partition.
  • Soft links. A soft link is something like a shortcut in Windows. It is an indirect pointer to a file or directory. Unlike a hard link, a symbolic link can point to a file or a directory on a different filesystem or partition.

How to use ln command

The ln command is used to make links between files. The command, by default, creates a hard link. To create a soft link simply append the -s option (--symbolic).
The ln command syntax for creating symbolic links is as follows:

  • If both the FILE and LINK are given, ln will create a link from the file specified as the first argument (FILE) to the file specified as the second argument (LINK).
  • If only one file is given as an argument or the second argument is a dot (.), ln will create a link to that file in the current working directory. The name of the symlink will be the same as the name of the file it points to.

By default, on success, ln doesn’t produce any output and returns zero.

How to create a symbolic link to a file

To create a symbolic link to a file, run the following command:

$ ln -s { OPTIONS } file symlink

When both the file and the symlink are defined, the ln command creates a link from the file which is the first argument to the file defined in the second argument symlink.
For example, to create a symbolic link to a file, use the syntax,

$ ln -s  original_file symlink

[box type=”note” align=”” class=”” width=””]Note: ln returns no output on success.[/box]
In the command, replace the original_file with the existing file for which you want to create the symlink and the symlink with the symbolic link.
Let’s have a real example:

$ ln -s file.txt sample_link.txt

The above command creates a symlink called ‘sample_link.txt‘ to the existing file ‘file.txt‘ in the current directory.
To verify the creation of the link, simply use the ls command as shown:

$ ls -l sample_link.txt

Your output should resemble what I have below:

lrwxrwxrwx 1 lintut lintut 8 Jul 25 23:26 sample_link.txt -> file.txt

In the permissions, the l flag indicates that this is a symbolic link, and the character – > indicates that the symlink points to the file file.txt.
Sometimes symlinks doesn’t work because of the path issues, suggest to use full path:

$ absolute (full path)
ln -s /path/to/originals/originalfile.txt backup/copy.txt

$ relative
cd backup
ln -s ../originals/originalfile.txt copy.txt

Creating Symlinks To a Directory

The command for creating a symbolic link to a directory is the same as when creating a symbolic link to a file. Specify the directory name as the first parameter and the symlink as the second parameter.
For example, if you want to create a symbolic link from the /mnt/my_drive/movies directory to the ~/my_movies directory you would run:

$ ln -s /mnt/my_drive/movies ~/my_movies

How to overwrite a symbolic link

Overwriting symbolic links by simply invoking the ln command without any additional arguments will always give you an error as shown:

For example, If you try running the following command again:

$ ln -s  file.txt   sample_link.txt

You will get the error shown:

ln: failed to create symbolic link 'sample_link.txt': File exists

A workaround to this issue is to introduce another option -f or --force to overwrite the symlink’s destination path as shown:

$ ln -sf  file.txt   sample_link.txt

This time, overwriting the soft link will not yield any error.

How to remove symlinks

To get rid of symbolic links Linux or symlinks, you can either use the rm command or the unlink command. And it’s quite easy. For unlink command, use the syntax below to remove symlinks:

$ unlink symlink_to_be_removed

Removing a soft link using the rm command is just the same as when you are deleting or removing a regular file:

$ rm symlink_to_be_removed


To create a symbolic link is Linux use the ln command with the -s option.
For more information about the ln command, visit the ln man page or type man ln in your terminal.
If you have any questions or feedback, feel free to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button