In this guide, we will go over what a symlink is on a Linux operating system and how to create and remove them.
At its very basics, a symbolic link (Sometimes referred to as a symlink) is a file that links to another file or directory on your Linux system. You could think of this as similar to a Windows shortcut, but they behave quite differently.
With a symlink, you can use both relative and absolute paths. This means you can reference a path in the directory above the link using “../file
“. You could reference it in a specific location like “/path/to/file
“.
These links are treated just like any other file on your system and are considered separate entities. Deleting a symbolic link on Linux won’t delete the source file and deleting the source file won’t remove the link.
There are a vast number of use cases for a symlink on Linux. For example, you can use a link to reference a file in another location without having multiple copies of that particular file.
In the following sections, we will show you how to create a symbolic link on your Linux system and how to delete them.
Creating a Symbolic Link to a File on Linux
Let us start by showing you how easy creating a symlink to a file within the Linux terminal is. To create and manage links, you will use the “ln
” command.
This command stands for “link” and is used to create both hard and symbolic links. Since we want to create a symbolic link, we must use the “-s
” option.
With this command, we first specify the “-s
” option to tell the terminal tool we want to create a symbolic link. Next, we specify the full or relative path to the file we want to link.
After specifying the path to a file, you want to type out what you want this link to be called.
ln -s /PATH/TO/FILE/TO/LINK SYMLINK_NAME
Example of Creating a Symlink
Let’s create a symlink between a kiosk script we wrote and our home directory to show you how this might work in practice.
A better use case for this might be to generate a link from your script to the “/usr/local/bin
” directory so you can call it directly, but we will use our home directory just to make it easier to show.
We will run the following command within the terminal to create this symbolic link on our Linux system.
ln -s /opt/kiosk/kiosk.sh ~/kiosk_script.sh
Once the link is created, let us verify it exists and the location it is linking to by using the ls command.
We could also just run the script from our home directory now rather than having to reference its full path.
ls -l ~
Looking at the output of the list command, you can see that the “kiosk_script.sh
” file is a symbolic link to the “/opt/kiosk/kiosk.sh
” script.
There are two ways you can identify a symlink on Linux. The first is by the “->
” symbols. The left side represents the symbolic link, and the right side reflects the file to which it is linked.
The other way to identify a symbolic link is that the permission string will start with the lowercase “l
” rather than a hyphen.
If the text is red for your link., this means that the link is to a file or folder that does not exist.
total 0
lrwxrwxrwx 1 pi pi 19 Sep 25 14:31 kiosk_script.sh -> /opt/kiosk/kiosk.sh
Using the Terminal to Create A Symlink Link to a Directory
On Linux, creating a symlink to a directory is the exact same as creating a symbolic link to a file.
You use the same “ln
” command and the same “-s
” option to create a symlink. However, instead of specifying a file as your link, you instead set a directory.
To help avoid confusing things, it’s usually a good idea to make sure that the symlink name is easily identifiable as a file.
ln -s /PATH/TO/DIRECTORY SYMLINK_NAME
Example of Creating a Symbolic Link to a Direcotry
For this example, we will create a symlink on our Linux system between the “/opt/kiosk
” directory and the “kiosk
” directory in our users home folder.
ln -s /opt/kiosk ~/kiosk
To see how our system sees this symlink between the two directories, we will list the contents of our home directory by using the command below.
ls -l ~/
Below is an example of what you would see after creating a symbolic link between two directories. You can identify a directory by the two symbols (->
).
Another way to tell whether a file is a link is the presence of the letter “l
” at the start of the permission string.
total 0
lrwxrwxrwx 1 pi pi 11 Sep 25 14:40 kiosk -> /opt/kiosk/
lrwxrwxrwx 1 pi pi 19 Sep 25 14:31 kiosk_script.sh -> /opt/kiosk/kiosk.sh
Deleting a Symbolic Link on your Linux System
As a symlink on Linux is just like any other file, you can delete it from your system using either the rm or unlink commands.
One key thing to note is that when you delete a symlink, you delete the link itself and not the file it is linked with. Likewise, if you delete a file that a link points to, the link itself will continue to exist; it will just now point to a nonexistent file.
For example, if we wanted to delete the “kiosk
” symbolic link we created in our home directory, we could just use the rm command as shown below.
rm ~/kiosk
Likewise, the unlink command can also be used. They both essentially achieve the same thing.
unlink ~/kiosk
One thing to be careful of is that even if you are deleting a symbolic link to a directory, you do not want to include the ending slash. A link is still a file, even if it points to a directory.
For example, the following would be incorrect when removing a symlink called “kiosk
“.
rm ~/kiosk/
Finding Broken Symlinks on Linux
As mentioned throughout this guide, a symbolic link can end up broken on Linux when the file or directory it is linked to is removed.
Luckily, we can use the forever useful find command to search a given directory for broken symlink. All we need to do is type “find
” followed by the path you want to search and then the option “-xtype l
“.
The “-xtype
” option is what makes thing whole thing work as it allows us to search for just symbolic links by setting the type as a lowercase L
(l
).
find <PATH> -xtype l
For this example, we intentionally moved the link file for our “kiosk
” example. So let us use the command above to scan our home directory.
find ~ -xtype l
After running this command, we could see that it easily found our broken symbolic link. We can now either repair it by adding the file it was linked to back. Alternatively, we can just remove it.
/home/pi/kiosk_script.sh
Finding and Delete Broken Symlink in One Go
You can take these things further using the find command to find and delete broken symlinks on Linux with a single command. This is useful if you have a ton of broken links you want to remove and aren’t worried too much about unintentionally deleting something you don’t want removed.
To change this behavior, we will be adding two options. The first is the “-delete
” option, which tells the tool to delete any file it finds. The second part is the “-print
” option so we can see each broken link that is removed.
find <PATH> -xtype l -delete -print
For this example, let us run it on our home directory.
find ~ -xtype l -delete -print
Below, you can see that our broken symbolic link was easily found and deleted. If we were to run the command again, we would see nothing printed to the terminal.
/home/pi/kiosk_script.sh
Conclusion
By this point in the guide, you should now understand how you can easily create and delete symlinks on a Linux-based system.
Symbolic links are a critical and incredibly useful part of the Linux filesystem. They allow you to easily make links between different parts of your filesystem.
If you have run into any issues with creating or removing one of these links, please feel free to comment below.
Now that you are done here, we recommend that you check out our many other Linux guides.