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.
Types of links
In Linux, we have 2 types of links: soft links and hard links.
Hard links: A hard link is a replica of an original file. It gives access to the data in the target file. A user cannot create a hard link for a directory, only for a file. Additionally, hard-linked files have a common inode number. When the original file is deleted or removed, the hard link will continue working and will contain the contents of the removed file. It’s also important to note that hard links do not span across different file systems.
Soft links: Unlike a hard link, a soft link is merely a pointer to a file name and does not contain the contents of another file or the target file being referenced. If the target file is removed or deleted, the soft link ceases to exist. The good side of soft links is that they can be used to link to a file or directory and they can span across different file systems.
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).
Syntax:
ln [OPTION] TARGET LINK_NAME
To avoid confusion, use absolute path (relative path) of the source and target file when creating a symbolic link.
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
Note: ln returns no output on success.
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 file1.txt sample_link.txt
The above command creates a symlink called ‘sample_link.txt’ to the existing file ‘file1.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 winnie winnie 9 Jul 12 23:43 sample_link.txt -> file1.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 file1.txt
.
Sometimes symlinks doesn’t work because of the path issues, suggest to use full path:
# absolute (full path) ln -s /path/to/originals/originalfile1.txt backup/copy.txt # relative cd backup ln -s ../originals/originalfile1.txt copy.txt
How to create symbolic links to a directory
Creating a symbolic link to a directory is just as easy as creating one to a file. The syntax remains largely the same. The first argument takes the directory’s name whilst the symbolic link is specified as the second argument.
The syntax below is an example of how you would go about this:
$ ln -s /path/to/directory ~/directory
For example, to create a symbolic link from the directory /Downloads/music/ to ~/my_music run the command:
$ ln -s /Downloads/music ~/my_music
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 file1.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 file1.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
Are symlinks important?
It’s generally a good idea to create a symlink to a file if you want to achieve any of the following:
- Have access to a file(s) from multiple locations without creating duplicate copies which can necessarily take up a lot of disk space.
- If you want to retain original versions of the file and ensure that the link points to the latest version of the file. This works because the symlink will still remain active even after you have replaced the file with a different file bearing the same file name.
Linux programs use symbolic links, as aliased so that users do not have to know which version of code used.
# which python /usr/bin/python # ls -l /usr/bin/python lrwxrwxrwx. 1 root root 7 Feb 21 13:38 /usr/bin/python -> python2
Conclusion
Symbolic links and standard shortcuts are similar but a few differences exist between the two.
A standard shortcut, like the one you’d find in Windows, is simply a regular file that points to a certain directory, file, or application. This shortcut usually appears as an icon that can be created in any location, and when clicked upon it opens the original file, directory, or application.
A symbolic link actually represents the original file in a functional sense. It’s a direct substitute for an object such as a file. Your Linux system reads the Linux symbolic link as though it was the target object. An ordinary shortcut simply references a file or a directory, and not does nothing else.
That’s all about how to create a symbolic link in Linux and for more information refer ln man. I hope you enjoyed reading and please leave your comments and suggestions.