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

A brief introduction of rm Command under Linux

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "a brief introduction to the rm command under Linux". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

The RM thing.

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

-r: Recursive, recursive operation on directories and their contents

-f: Force, no confirmation required

These two options can be written separately as-r and-f, or they can be combined into-rf, following POSIX conventions. The-r and-f options here are not the spoofed "Rubbish" and "Files" in the above picture.

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

-r option

The-r option stands for recursively, which means recursively deleting directories and files or subdirectories under them in the parameter.

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

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

-f option

Before introducing the-f option, let's look at the-i option, which doesn't appear here.

The native rm command follows UNIX conventions when deleting files or directories, and is silent and silent before and after the delete operation. Never complain unless you encounter an error, such as a file that doesn't exist.

Later, probably because many people delete files by mistake, 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 prompt before each deletion action and requires explicit confirmation from the user before deletion.

However, sometimes this prompt is too annoying, so many people deliberately override the behavior of the-i option with the-f option when operating, making the definition of this alias meaningless.

Therefore, there is also the-I option, which prompts for confirmation when deleting three or more files or recursively. This is less annoying than the-i option and prevents most misoperations. Therefore, you can use the-I option for the above alias and avoid using the-f option.

* What's the wildcard?

We see * in the command to refer to all files in the directory. But strictly speaking, the wildcard * means no dot. "All files at the beginning. With "... "The first file is a hidden file under Linux by default.

Therefore, this command does not delete the/directory with. Hidden files at the beginning, and. And... Two catalogues. But in recursive operation, it recursively deletes subdirectories except. And... All files and subdirectories outside the directory--whether or not they are marked with. Beginning--because recursive operations are not wildcarded by shells such as Bash.

As for why not delete the contents of the directory when will also. And... Equal treatment? Because since 1979 when the rm command began to have the ability to delete directories, it specifically avoided these two special directories.

root directory protection

As experienced system administrators may recall at this point, the rm command has a pair of root-specific options--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: does not protect the root directory.

This pair of options was later added to the rm command. Probably almost every system administrator has made an error, and many of them have deleted the root directory (I'm one of them). There are several reasons for this:

Input hand error: For example, I originally wanted to enter rm/tmp/test.txt, but I accidentally hit the keyboard and entered a space to become: rm /tmp/test.txt. See the space after the root directory (/)?!-- That's the mistake I've made myself so far, and it's on the production server.

improperly initialized or incorrectly named shell

script

Variable: For example, in

script

rm -rf /${tmp_dir}, if either the tmp_dir variable is incorrectly assigned or incorrectly typed (it might have been tmpdir?) What does that lead to? Of course, delete the root directory ~

Given the number of such cases, it's almost as good a joke in Linux circles as "How to quit vi for beginners." So, in POSIX Version 7, the rm command added the--preserve-root option as the default behavior to reduce the likelihood of this error.

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

Some students may ask, then why is there a special--no-preserve-root option? This is probably primarily a UNIX philosophy, which gives you all the power you want and makes it your business to be stupid, not the OS's business. What if you really want to delete all the files in the root directory?

You don't have to say that there is such a need: for example, you want to erase all files in a chroot environment. chroot We don't talk much about it here, it is to use a directory as a "prison," this directory logically forms a new "root directory," file operations in this prison can not cross the directory category. Docker, LXC/LXD and other popular container technologies in recent years are all chroot technologies.

UEFI system

Well, you might be a little more maverick, and that's to purge all files in the root directory of the physical environment! But before you hit Enter, think again, are you on a UEFI system?

Because UEFI systems map their firmware, variables, and settings to the/sys partition under the root directory, if you clear everything under the root directory in a UEFI environment, you will also clear/sys, which may cause you to lose UEFI estimates and bricking the device.

sudo

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

After entering this command, a password will be requested. Whose password? Not root password, but password of the current user who entered the command.

For more information about who can execute sudo and what he can execute with sudo, see our other articles. Incidentally, remember to distinguish between sudo and su commands.

junk files

At this point, we can't forget the original intention of this command, deleting "junk files."

Do you have junk files under Linux? Yes. These junk files generally come from:

There are no orphan files managed by package manager, which are left in the system after the package is deleted

Useless dependencies, not deleted after the software that needs them is deleted

Temporary files not cleaned up

Legacy diagnostic files

Do these junk files need to be cleaned up? In general, most of these files on Linux systems do not have any impact on the health of the system, unless they are too many and take up a lot of storage space and inodes.

So, if you feel like your Linux system is slowing down, it's almost certainly not due to junk files, at least at this point, the experience from Windows isn't worth copying.

"Linux rm command under a simple introduction" to the content of the introduction here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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