top of page

Posts

The Reason Why the Unlink Command is Better to Delete Symbolic Links



Hello,


Recently I had a chance to deal with symbolic links when I had to renew an SSL certificate.

Therefore, when I was doing research about symbolic links, I saw a lot of information about “to delete symbolic links, use unlink command”.

But, the following information caught my attention,


“Not many people explain about unlink command that is just RELATIVELY safer to use for deleting symbolic links, but if you make mistake, it’s going to be very bad”

“The way of use the command and options are slightly different depending on the person”

“Some people misunderstood that unlink command is just for deleting symbolic links”


So, in this article, I’m going to explain what the unlink command is and how and why we use it to delete symbolic links.


Execution Environment

OS: Ubuntu 20.04.5 LTS (WSL2)

Shell: bash


Important Note

In this article, I’m going to talk about RHEL and Debian which are the main distributions for Linux.

UNIX systems such as Solaris have different specifications for the unlink command.

unlink is a command to delete files

Some people might be considering un“link” command is used to delete the symbolic “link”, but it is actually “a command that can also delete symbolic links but it’s primarily for deleting files”.

If I use man in Ubuntu, the explanations show up like the following



$man unklink
UNLINK(1) User Command UNLINK(1)
NAME
unlink - unlink call a function and delete the specified file
FORMAT
unlink FILE
unlink OPTION
DESCRIPTION
Call unlink function to delete specified FILE
--help display usage and close
--version
Display the version information and close
※omit below


As described above, the unlink command is for “deleting specific files”.

Because it’s not a command for deleting symbolic links, it can delete files of entities as well.


The internal behavior of deleting files is the same as the rm command

Description: Call the unlink function to delete the specified files.


The “unlink function” described above(man) refers to “unlink”, a "system call" that executes an internal underlying Linux kernel function.

The kernel receives this system call, and deletes the hard links in the specified files, which makes the data disappear.

I’m going to omit to describe details of the hard link and the symbolic link of the Linux file structures because that will be very long, but after all, it calls an internal function to delete data, and this system call is also used for the rm command.


The "rm" command is a combination of the "rmdir" system call for deleting directories and the "unlink" system call for deleting files with various optional functions.


Regarding deleting files, the rm command and the unlink command are doing the same thing in the internal Linux kernel.


Therefore I believe that considering the unlink command as a command for deleting files would be safer.



Create an entity file for deletion as a test to see how it works

Created a file named “Testdate” in my test environment.




$ touch Testdate
$ ls -l
Total 0
-rw-r--r-- 1 ubuntu ubuntu 0 Sep 16 17:10 Testdate


Now, I can see that the file is deleted by the unlink command.



$ unlink Tstdate
$ ls -l
Total 0


Since this command is for deleting entity files, even if you try to delete just the symbolic links but you specified the original link source file, it will delete the file.


Also, since there is no feature like the -i option for the rm command which prompts before every removal, you have to be careful when you delete files with the unlink command.



What is the difference between the rm command and the unlink command?

The unlink command cannot delete directories and cannot recursively delete the files that are in the directory


This is the standard reason why this command is recommended to be used to delete symbolic links.


It prevents deleting “link source directory” or “symbolic link destination distribution” by accident with the rm command.


Let me explain this.

I created a “TestDir” directory and a “TestDir-link” symbolic link.

Under “TestDir” I placed three text data files.



$ ls -l
Total 4
drwxr-xr-x 2 ubuntu ubuntu 4096  9 16 18:01 TestDir
lrwxrwxrwx 1 ubuntu ubuntu	7  9 16 17:53 TestDir-link -> TestDir
 
$ tree
.
├── TestDir
│   └── {TestDate1.txt}{TestDate2.txt}{TestDate3.txt}
└── TestDir-link -> TestDir


Examples of “Dangerous rm command mistakes which people tend to make”


BAD: Specified the directory which symbolic link is pointing


$ rm -rf TsetDir

This commonly happens when the directory’s name and symbolic link’s name are similar.

and of course, the whole directory is gone now.

It drives you to despair.


BAD: Specified the path by adding “/” a the end of “TestDir-Link”


$ rm -rf TsetDir-link/

This is the most common mistake. If you put “/” at the end, it means it’s pointing at “symbolic link’s destination”, not the symbolic link itself.


In this case, it deletes all the files that are in the directory which are

{TestDate1.txt}{TestDate2.txt}{TestDate3.txt} and these files are gone.


If you wonder, like “Oh? the symbolic link is still here…?”, and check inside the directory, you’d be so depressed because the files are no longer there.


First of all, when you use the rm command, you should be very careful with using the -f option. However, there are cases that people just got used to the environment that they feel too comfortable with using the option and the command.


The unlink command would be failed to delete the files

If you use the unlink command, you have no risk of the incident as I explained above.

Let’s try that out.



$ unlink TsetDir
unlink: Cannot delete'TsetDir' (unlink) : There’s no such file or directory

This command is not allowed to delete a directory that it cannot delete the specified linked directory in this case.



$ unlink TsetDir-link/
unlink:  Cannot delete'TsetDir/' (unlink) : There’s no such file or directory

It also cannot process it recursively so it cannot delete files inside of the directory.


Because of this, it’d be safer to use the unlink command to delete compared to using the rm command.


Summary: The reason why the unlink command is recommended to be used to delete symbolic links


If you use -i option with the rm command, you have to be careful but you can still use it to delete symbolic links.

However, everyone makes mistakes so it’s impossible to eliminate failure.

Then it’s important to make systems that “Cannot go wrong”.


This concept is called “foolproof”, as an operation based on this concept, using the unlink command is recommended because the command has limited functions and less risk of making mistakes.


That’s all that I wanted to share with you.

If you take a deeper look at “why the unlink command is recommended”, you’ll see “data structure of Linux” and “how foolproof is important”.


I hope that this article will help to reduce the incidents of symbolic link deletion by accident, and I also hope that this article will make you interested in the structures and safety of Linux.


Thank you for reading this article!



This blog post is translated from a blog.post written by Naka Tomo on our Japanese website Beyond Co..

bottom of page