Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to clean up garbage on Linux system administrator

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to clean up the garbage system administrator on Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.

What happened with rm.

First of all, this command is used to clear all files under the root directory (/) of the Linux system. It uses two command options:

-r:

Recursive (recursive)

To perform recursive operations on the directory and its contents

-f:

Mandatory (force)

, no need to confirm the operation

These two options can be written separately as-r and-f, or you can combine the two options into-rf according to POSIX conventions. The-r and-f options here are by no means the spoof "Rubbish" and "Files" in the image above.

The following parameter / * refers to all files in the root directory.

-r option

The-r option stands for recurive, which means to recursively delete the directory in the parameter and the files or subdirectories under it.

In addition to the short option style, this option also has a GNU-style long option-recursive; it also supports the synonymous uppercase-R parameter for compatibility reasons.

Without this option, the directory and its contents are not deleted.

-f option

Before we introduce the-f option, let's take a look at the-I option that doesn't appear here.

Native rm commands follow UNIX conventions when deleting files or directories and are silent and unprompted before and after a successful delete operation. Never complain unless you encounter an error, such as the file you want to delete does not exist.

Later, perhaps because many people often erroneously delete files, the rm command on most Linux distributions is an alias with the-I option added:

Alias rm=rm-I

The-I option here is used to give a prompt before each delete action, which will not be deleted until it is explicitly confirmed by the user.

However, sometimes this prompt is so annoying that many people deliberately use the-f option to override the behavior of the-I option, making the definition of this alias meaningless.

Therefore, there is also a-I option, which gives a confirmation prompt when you want to delete three or more files or delete them recursively. In this way, it is not as annoying as the-I option, but also prevents most misoperations. Therefore, you can use the-I option for the above aliases and avoid using the-f option.

* what is the wildcard?

We see that the command refers to all the files in the directory with *. But strictly speaking, the wildcard "*" means no dot. " All the files at the beginning. With "." The file at the beginning belongs to the hidden file under Linux by default.

Therefore, this command will not delete / directory to. The hidden file at the beginning, as well. And... Two directories. However, in a recursive operation, the subdirectory is recursively deleted except. And... All files and subdirectories outside the directory-- whether with or without. Beginning-- because recursive operations are not wildcard unfolded by shell such as Bash.

As for why not delete the contents of the directory will also. And... Give everyone his due? Because these two special directories have been specifically avoided since the rm command began to have the ability to delete directories in 1979.

Root directory protection

Experienced system administrators may recall that the rm command has a pair of options specific to the root directory-- preserve-root and-- no-preserve-root. This pair of options means:

-- preserve-root: protects the root directory, which is the default behavior.

-- no-preserve-root: the root directory is not protected.

This pair of options were later added to the rm command. It is possible that almost every system administrator has made operational errors, and there are plenty of people who have deleted the root directory (I am one). There are several reasons for this:

Input error: for example, I wanted to type rm / tmp/test.txt, but I accidentally hit the keyboard and typed one more space into: rm / tmp/test.txt. See the space behind the root directory (/)? This is the mistake I have made myself right now, and it is on the production server.

Incorrectly initialized or incorrectly named shell script variables: for example, in the script, rm-rf / ${tmp_dir}, whether the tmp_dir variable is not correctly assigned or typed incorrectly (originally or tmpdir? ), what would that lead to? Delete the root directory, of course.

In view of the endless emergence of this situation, it has become almost as a classic joke in Linux circles as "how beginners quit vi". So, in the seventh edition of the POSIX specification, the rm command adds the-- preserve-root option as the default behavior to reduce the likelihood of such an error.

However, this option does not prevent the operation described in this article to clear all files (/ *) in the root directory.

Some students may ask, then why is there a special no-preserve-root option? This may be mainly because of UNIX philosophy, giving you all the power you want, and being stupid is your business, not the operating system's. What if you really want to delete all the files in the root directory?

Not to mention, there is such a need: for example, you want to erase all files in a chroot environment. Chroot we do not say much here, it is a directory as a "prison", the directory logically formed a new "root directory", the file operations in the prison can not go beyond the scope of this directory. Container technologies such as Docker and LXC/LXD, which are popular in recent years, are all chroot technologies.

UEFI system

Well, you may be more maverick, which is to clean up all the files in the root directory in the physical environment! But before you press enter, please think again, are you on a UEFI system?

Because the UEFI system maps its firmware, variables, and settings to the / sys partition under the root directory, if you clear all the contents of the root directory in the UEFI environment, it will also clear / sys, which may cause you to lose the estimated settings of UEFI, thus making the device brick.

Sudo raises the right

In order to delete files belonging to systems such as root and other users, this command also needs to be preceded by sudo to claim rights.

After you enter the command, you are asked for a password. Whose password? Not the root password, but the password of the current user who entered the command.

As for who can execute sudo commands and what commands he can execute through sudo commands, I will not repeat them here. Please refer to our other articles. By the way, remember to distinguish between the connections and differences between the sudo and su commands.

Junk file

At this point, we must not forget the original intention of this command and delete "junk files".

Are there any junk files under Linux? Yes. These junk files generally come from:

Orphan files that are not managed by the package manager are left in the system after the package is deleted

Useless dependent packages, which are not deleted after the software that needs these dependent packages is deleted

Temporary files without cleanup

Legacy diagnostic files

So do these junk files need to be removed? Generally speaking, most of these files on Linux systems will not have any impact on the healthy operation of the system, unless there are too many, taking up a lot of storage space and inode.

So, if you feel that your Linux system is slow, it's almost certainly not caused by junk files, at least at this point, the experience from Windows systems is not worth replicating.

Thank you for reading this article carefully. I hope the article "how to clean up the garbage system administrator on Linux" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report