[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

Why use the unlink command to "remove symbolic links"

Hello. I am a member of the System Solutions Department.
Recently, I had the opportunity to use symbolic links to update my SSL certificate.

At that time, when I looked into symbolic links again, I found a lot of information about using the unlink command to delete symbolic links.
However, I was concerned about the following points.

"Deleting symbolic links is only ``relatively" safe, but sometimes they don't explain that if you make a mistake, it can be dangerous.'' ``The
way people use commands and options differs slightly.'' `
`Sometimes deleting symbolic links Some people misunderstand it as a command for

Therefore, in this article,
I would like to explain why and how to use the unlink command to ``remove symbolic links'', including what kind of command it is.

Execution environment

・OS: Ubuntu 20.04.5 LTS (WSL2 environment)
・Shell: bash
・Locale changed to Japanese

Precautions

This article is for 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.

unlink command is a command to delete files

Since it is written as un "link", some people may misunderstand it as a command to delete a symbolic "link", but

``It can only delete symbolic links, but it is originally a command for deleting files.''

If you check it by hitting man on Ubuntu, you will see the following explanation.

$man unklink UNLINK(1) User command UNLINK(1) Name unlink - Calls the unlink function to delete the specified file Format unlink FILE unlink OPTION Description Calls the unlink function to delete the specified FILE. --help Display usage information and exit --version Display version information and exit *The following is omitted.

As you can see, the command says ``Delete the specified file.''

Since this is not a command for deleting symbolic links, it means that you can also delete actual files normally.

The internal behavior when deleting files is the same as the rm command.

Description Calls the unlink function to remove the specified FILE.

The "unlink function" explained in the above (man) refers to "unlink", a "system call" that executes the internal core Linux kernel function.
When the kernel receives this system call, it deletes the hard link of the specified file, effectively erasing the data.

I will not go into details because it is a fundamental and lengthy explanation of Linux file structures and hard links and symbolic links, but
in short, this system call calls a function that deletes data internally, and this system call also uses the rm command. I am.

uses the "rmdir" system call to delete directories and the "unlink" system call to delete files, and
incorporates various optional functions into them.

In other words, when it comes to deleting files, the rm command and unlink command do the same thing internally in the Linux kernel.

I believe that having a strong sense of crisis (a sense of crisis) as a command to delete files is a good way to ensure safety.

As a verification, create an actual file for deletion and check the operation

Prepare a file called "Testdate" in the verification environment for verification.

$ 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 the actual file using unlink.

$ unlink Tstdate $ ls -l total 0

Since this is a command that deletes an actual file, if you try to delete a symbolic link and specify the original link source file, it will naturally disappear.

Also, unlike the rm command, it does not have a function to prevent misoperations like the -i option, so be careful.

How is it different from the rm command?

The unlink command cannot remove directories, nor can it recursively remove files within directories.

This is why it is generally recommended to use this command when removing symbolic links.

This is because it prevents deletion accidents when you accidentally specify the ``link source directory'' or ``specify the directory under the symbolic link destination'' with the rm command.

In order to explain what is going on with a failure example,
I have prepared a directory called "TestDir" and a symbolic link "TestDir-link" to it.
Three files of text data 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 are often done"

BAD: I specified the link source directory

$ rm -rf TsetDir

This is a case where the "link source directory" is specified, which tends to occur when the name of the link source directory and the symbolic link are similar.
Naturally, the entire directory will be deleted.

Despair comes.

BAD: I specified the path by adding "/" after the symbolic link "TsetDir-link" that I wanted to delete.

$ rm -rf TsetDir-link/

This is the most common mistake to make. If you add a ``/'' when specifying a directory, you
are specifying ``under the symbolic link destination'' instead of a symbolic link.

In this case, the directory will be recursively targeted, and
all data in {TestDate1.txt}{TestDate2.txt}{TestDate3.txt} will disappear temporarily.

When I checked the contents of the directory, I felt despair, thinking, "I can't delete the symbolic link."

*In the first place, the -f option of the rm command is a dangerous item that should not be used lightly, but
if you are used to it, you may end up using it unconsciously, so be careful.

If you use the unlink command, it will not fire and accidents will be less likely to occur.

In the above dangerous case, the unlink command will fail due to its specifications.
Let's try it out.

$ unlink TsetDir unlink: Cannot remove (unlink) 'TsetDir': No such file or directory

This is because the directory cannot be deleted, so the link source directory will not be deleted directly.

$ unlink TsetDir-link/ unlink: Cannot remove (unlink) 'TsetDir-link/': No such file or directory

Basically, it cannot be deleted without directly specifying the file name, so
the directory cannot be processed recursively, so the files in the directory will not be deleted.

 

In this way, the unlink command is designed to fail, so the risk of accidents is considerably reduced compared to using rm.

Summary: Why the unlink command is recommended for removing symbolic links

The rm command can also be used to remove symbolic links if you are careful and include the -i option.

However, humans are creatures that make mistakes, and it is impossible to eliminate them,
so in order to prevent accidents, we need a system that prevents people from making mistakes in the first place and prevents mistakes from becoming mistakes.

This concept foolproofing , and when operating based on this
, use unlink, a command with limited functionality and less impact than rm, which has a large impact when a mistake occurs. That's what they say.

So, that's all for the explanation of this article.
If you dig deeper into ``why unlink is recommended,'' you will learn about ``the structure of data in Linux'' and ``the importance of foolproofing.''

It would be great if this article could help reduce the number of symbolic link deletion accidents, and I also hope that you would become interested 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

If you found this article helpful , please give it a like!
24
Loading...
24 votes, average: 1.00 / 124
19,696
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

inside

Beyond mid-career in 2022 Belongs to
the System Solutions Department
LPIC-3 I have a 304 and AWS SAA I only
have three choices for regular drinks: milk, cola, and black tea.