Read in 3 minutes
How to Create Symbolic Links in Linux Using the ln Command
A symbolic link, also known as a symlink or soft link, is a special type of file that points to another file or directory.
In this guide, we will cover how to use the ln command to create symbolic links.
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 for a file on a different filesystem or partition.
- Soft links. 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.
ln is a utility for creating links between files. By default, the
ln command creates hard links. To create a symbolic link use the
ln command syntax is as follows:
ln [OPTIONS] FILE LINK
To create a symbolic link in Linux, open your terminal and type:
ln -s source_file symbolic_link
source_file with the name of the existing file for which you want to create the symbolic link and
symbolic_link with the name of the symbolic link.
symbolic_link parameter is optional. If you do not specify the symbolic link, the ln command will create a new link in your current directory.
Creating Symlinks To Files
In the following example we are creating a symbolic link named
my_link.txt to a file named
ln -s my_file.txt my_link.txt
To verify that the symlink was created use the
ls -l my_link.txt
The output will look something like this:
lrwxrwxrwx 1 linuxize users 4 Nov 2 23:03 my_link.txt -> my_file.txt
l character is a file type flag that represents a symbolic link. The
-> symbol shows the file the symlink points to.
Creating Symlinks To Directories
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
If you try to create a symbolic link that already exists, the ln command will print an error message.
ln -s my_file.txt my_link.txt
ln: failed to create symbolic link 'my_link.txt': File exists
To overwrite the destination path of the symlink use the
ln -sf my_file.txt my_link.txt
You can delete/remove an existing symbolic link using either the
unlink command syntax is very simple:
Removing symbolic link using the rm command is the same as when removing a file:
You should prefer using the
unlink utility for removing a symbolic link.
If you delete or move the source file to a different location, the symbolic file will be left dangling. You should delete it because it will no longer work.
By now you should have a good understanding of how to use the Linux ln command and how to create symlinks.