Why use the unlink command to "delete symbolic links"?
![]()
table of contents
Hello. I'm Naka from the System Solutions Department.
Recently, I had the opportunity to work with symbolic links in order to renew an SSL certificate.
While researching symbolic links again, I came across a lot of information stating that "the unlink command is used to delete symbolic links."
However, the following points concerned me.
"Sometimes it's not explained that deleting symbolic links is only 'relatively' safe, and can be quite dangerous if done incorrectly."
"The way commands and options are used varies slightly from person to person."
"Some people occasionally misunderstand the command for deleting symbolic links."
Therefore, in this article,
I would like to explain what the unlink command is, so that you can understand why and how to use it to "delete symbolic links".
Execution environment
• OS: Ubuntu 20.04.5 LTS (WSL2 environment)
• Shell: bash
• Locale changed to Japanese
Precautions
This article covers the major Linux distributions, RHEL and Debian
This is because there are differences in the specifications of the unlink command in UNIX systems such as Solaris
The unlink command is used to delete a file
Since it says un "link", some people may mistakenly think that it is a command to delete a symbolic "link", but
"It can also delete symbolic links, but it is actually a command for deleting files."
If you type man in Ubuntu and check, you will find the following explanation:
$man unklink UNLINK(1) User command UNLINK(1) Name unlink - Calls the unlink function and deletes the specified file Format unlink FILE unlink OPTION Description Calls the unlink function to delete the specified FILE. --help Displays this usage and exits --version Displays version information and exits *Omitted
As the description says, it is a command to "delete the specified file."
Since this is not a command for deleting symbolic links, you can also delete the actual files
The internal behavior when deleting a file is the same as the rm command
Description Calls the unlink function to delete the specified FILE
The "unlink function" described above (in the man page) refers to the "unlink" system call, which executes a core function of the Linux kernel.
The kernel receives this system call and deletes the hard link to the specified file, effectively erasing the data.
I won't go into detail here, as it would require a fundamental and lengthy explanation of Linux file structures and hard/symbolic links.
In short, it invokes a function that internally deletes data, and this system call also uses the `rm` command.
the `rmdir` system call for deleting directories and the `unlink` system call for deleting files, incorporating
The `rm` command is an enhanced version of
In other words, when it comes to deleting files, the rm command and the unlink command do the same thing internally in the Linux kernel
The author believes that being aware (and having a sense of crisis) that this is a command to delete files will lead to safety
To verify this, create a file for deletion and check the operation
For verification purposes, prepare a file called "Testdate" in the verification environment
$ touch Testdate $ ls -l total 0 -rw-r--r-- 1 ubuntu ubuntu 0 Sep 16 17:10 Testdate
Here, you can confirm that you can delete the actual file by specifying it using unlink
$ unlink Tstdate $ ls -l total 0
Since this is a command that deletes the actual file, if you try to delete a symbolic link and specify the original link source file, it will naturally be deleted
Also, unlike the rm command, it does not have a function to prevent accidental operation such as a confirmation prompt like the -i option, so be careful
How is it different from the rm command?
The unlink command cannot delete directories, and cannot recursively delete files within directories
This is why it is generally recommended to use this command to remove symbolic links
This is because it prevents accidental deletion when you accidentally specify the "source directory" or "subdirectory of the symbolic link destination" using the rm command
To illustrate what this means with a failure example,
I have prepared a directory called "TestDir" and a symbolic link to it called "TestDir-link".
Three text data files for verification are placed under "TestDir".
$ ls -l total 4 drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 16 18:01 TestDir lrwxrwxrwx 1 ubuntu ubuntu 7 Sep 16 17:53 TestDir-link -> TestDir $ tree . ├── TestDir │ └── {TestDate1.txt}{TestDate2.txt}{TestDate3.txt} └── TestDir-link -> TestDir
Introducing "Dangerous rm command failure cases that you may make"
BAD: The link source directory was specified
$ rm -rf TsetDir
This is a case where the "source directory" is specified, which often occurs when the name of the source directory and the symbolic link are similar.
Naturally, the entire directory will be deleted.
Despair comes
BAD: I want to delete the symbolic link "TsetDir-link", but I specified the path with a "/" after it
$ rm -rf TsetDir-link/
This is the most common mistake people make: when specifying a directory, if you accidentally add a "/", you
're specifying "a subdirectory to the symbolic link" rather than a symbolic link itself.
In this case, the entire directory is recursively targeted, and
all data in {TestDate1.txt}{TestDate2.txt}{TestDate3.txt} is simply deleted.
"Huh? The symbolic link won't disappear?" you think, and despair sets in when you check the contents of the directory
*The -f option with the rm command is a dangerous tool that should never be used lightly, but
it seems that in familiar environments, it can sometimes be used unconsciously, so it's something to be careful about.
The unlink command will fail, so accidents are less likely to occur
In the dangerous scenario described above, the `unlink` command will fail due to its specifications.
Let's try running it.
$ unlink TsetDir unlink: cannot unlink 'TsetDir': No such file or directory
This is because the specification does not allow directories to be deleted, so the source directory of the link will not be deleted directly
$ unlink TsetDir-link/ unlink: cannot unlink 'TsetDir-link/': No such file or directory
Because the system is designed so that files cannot be deleted unless the filename is specified directly,
it cannot recursively process files within a directory, and therefore will not delete the files inside that directory.
As you can see, the unlink command is designed to fail, so the risk of accidents is significantly reduced compared to using rm
Summary: Why the unlink command is recommended for removing symbolic links
You can also use the rm command to delete symbolic links if you use the -i option and take care
However, since humans are creatures that make mistakes and "it's impossible to eliminate them,"
then in order to prevent accidents, "a system is needed that either prevents people from making mistakes in the first place, or prevents mistakes from becoming serious mistakes."
This concept isfoolproofingcalled
and when working under this principle, you're advised to use the `unlink` command, which has limited functionality and less impact when mistakes occur, rather than the `rm` command, which can have a significant impact if mistakes happen.
That concludes the explanation in this article.
If we delve deeper into "why unlink is recommended," we can touch upon "data structure in Linux" and "the importance of foolproofing."
I hope that this article will help reduce the number of accidents involving symbolic link deletion, and that it will also pique your interest in the structure and security of Linux
Thank you for reading this far!
Reference materials
unlink(1) - Arch manual pages
https://man.archlinux.org/man/unlink.1.en
27
