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 to update my SSL certificate.
When I looked into symbolic links again, I found a lot of information that said "Use the unlink command to delete a symbolic link."
However, I was concerned about the following points:
"Deleting symbolic links is only "relatively" safe, but they don't explain that it can be dangerous if you make a mistake."
"The way commands and options are used varies slightly from person to person."
"Sometimes people mistakenly think it's a command for deleting symbolic links ."
So in this article,
I would like to explain what the unlink command is, so that you can understand why it is used to "delete symbolic links" and how to use it.
Execution environment
・OS: Ubuntu 20.04.5 LTS (WSL2 environment)
・Shell: bash
・Change locale 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" explained above (man) refers to the "system call" called "unlink" that executes the underlying Linux kernel function.
The kernel receives this system call and deletes the hard link of the specified file, effectively deleting the data.
I won't go into details here as it would be a lengthy and fundamental explanation of Linux file structures and hard links/symbolic links, but
basically it internally invokes a function to delete data, and this system call is also used by the rm command.
uses the "rmdir" system call to delete directories and the "unlink" system call to delete files,
and adds various optional functions to these.
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 explain what this means with a failure example,
we have prepared a directory called "TestDir" and a symbolic link to it called "TestDir-link."
Three text data files for verification purposes 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 the case where the "link source directory" is specified, which is likely to occur when the link source directory and the symbolic link have similar names.
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; if you accidentally add a "/" when specifying a directory, you
will end up specifying "the directory under the symbolic link's destination" rather than a symbolic link.
In this case, the directory will be recursively searched, and
all data in {TestDate1.txt}{TestDate2.txt}{TestDate3.txt} will disappear.
"Huh? The symbolic link won't disappear?" you think, and despair sets in when you check the contents of the directory
*In the first place, the -f option with the rm command is a dangerous and dangerous option that should not be used lightly, but
it seems that there are cases where you ``accidentally'' use it unconsciously in an environment you are familiar with, so it is important to be careful.
The unlink command will fail, so accidents are less likely to occur
In the dangerous cases 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
Basically, you cannot delete a file unless you specify the file name directly, so
it cannot process directories recursively and will not delete the files in the 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 is "impossible to eliminate this,"
in order to prevent accidents, "a system is needed that prevents people from making mistakes in the first place, or that prevents mistakes from becoming mistakes even if they do occur."
This concept is called
foolproofing and when working based on this, it is said that you should use the unlink command, which has limited functionality and is less likely to have an impact than rm, which has a greater impact when a mistake occurs.
That concludes the explanation for this article.
If we dig deeper into why unlinking is recommended, we can touch on the 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