In Linux systems, deleting files is a very dangerous operation, especially when the files you want to delete are located in the system library or running directory. Deleting these files may lead to system crashes, application errors, and other serious consequences. Today we will introduce how to safely delete libraries and running files in Linux systems to avoid unnecessary risks.
I often see on the Internet that some operators or programmers accidentally typed the wrong rm delete function, causing the system and database to be deleted. As for running away, today the editor will take you to have an in-depth understanding of this deletion and running away.
I recently saw a picture on the Internet (the source of the original picture is unknown, the title picture is reproduced from the original picture, with modifications.)
It feels very reasonable. You see, rm means delete. This word is simple and easy to remember; even rf has an explanation, "junk files"; /* represents all files in the directory, which is fine; sudo is also available, Make sure the permissions are OK.
Will anyone execute such a command to clean up so-called junk files? Don't tell me, it's really hard to say.
We often post articles about Bash aliases, and some students always "contribute" some unique aliases, such as alias ls=rm -rf, alias cd=rm -rf, etc. Although I think almost no one is fooled by these commands, everyone often enjoys them.
I sent this picture to my circle of friends, and my friend "Long Shisan" said that this not only removes junk files, but also removes junk system administrators.
Yes, why do jokes like this appear again and again? It's because some of us who don't want to understand the problem, when we encounter a problem, are used to randomly finding command examples from the Internet and trying them blindly, without exploring its principles or verifying its reliability. Therefore, jokes like this are really used to clear out "junk in the system administrator".
So let's explore the above command and see how much knowledge there is here.
Those things about rm
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, perform recursive operations on the directory and its contents
-f: force, no confirmation required
These two options can be written separately -r and -f, or according to POSIX convention, the two options can be combined into -rf. The -r and -f options here are definitely not the "Rubbish" and "Files" spoofs in the picture above.
The following parameters /* refer to all files under the root directory /.
-r option
The-r option represents recursive, 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 the GNU style long option –recursive; for compatibility reasons, the synonymous uppercase -R parameter is supported.
Without this option, the directory and its contents will not be deleted.
-f option
Before introducing the -f option, let us first take a look at the -i option that does not appear here.
The native rm command follows UNIX conventions when deleting files or directories. It is silent and silent before performing the deletion operation and after the operation is successful. Never complain unless you encounter an error (such as the file you want to delete does not exist).
Later, probably because many people often 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 make a prompt before each deletion action, and the user needs to give explicit confirmation before deletion.
However, sometimes, this prompt is really annoying, so many people deliberately use the -f option to override the behavior of the -i option during operation, making the definition of this alias meaningless.
Therefore, there is also a -I option, which will prompt a confirmation when you want to delete three or more files or delete recursively. This is less annoying than the -i option and prevents most incorrect operations. Therefore, you can use the -I option for the above aliases and avoid using the -f option.
*** What is wildcard**
We see that * is used in the command to refer to all files in the directory. But strictly speaking, the * wildcard character represents all files that do not start with a dot "." Files starting with "." are hidden files under Linux by default.
Therefore, this command will not delete the hidden files starting with . in the / directory, as well as the . and .. directories. However, during the recursive operation, all files and subdirectories in the subdirectory except the . and .. directories will be deleted recursively - regardless of whether they start with . - because the recursive operation is not wildcard expansion by shells such as Bash.
As for why not treat . and .. equally when deleting the contents of a directory? Because since 1979, when the rm command began to have the ability to delete directories, it has specifically avoided these two special directories.
Root Directory Protection
System administrators with certain experience may remember at this time that the rm command has a pair of options specifically for the root directory –preserve-root and –no-preserve-root. The meaning of this pair of options is:
–preserve-root: Protect the root directory, this is the default behavior.
–no-preserve-root: Do not protect the root directory.
This pair of options was added to the rm command later. Probably almost every system administrator has made operational mistakes, and many of them have deleted the root directory (I am one). There are several reasons why this happens:
Input errors: For example, I originally wanted to enter rm /tmp/test.txt, but accidentally the keyboard flew up, and I entered an extra space and it became: rm / tmp/test.txt. Did you see the space after the root directory (/)? ! ——This is the mistake I have made myself currently, and it was on a production server.
Incorrectly initialized or incorrectly named shell script variables: For example, in the script, rm -rf /${tmp_dir}, if the tmp_dir variable is not assigned correctly or is entered incorrectly (perhaps it was originally tmpdir?), it will cause What? Of course, delete the root directory~
In view of the endless occurrence of this situation, it has become almost as classic a joke in the Linux circle as "How do beginners exit vi". Therefore, in the POSIX seventh edition specification, the –preserve-root option was added to the rm command as the default behavior to reduce the possibility of this error.
However, this option does not prevent the clearing of all files in the root directory (/*) described in this article.
Some students may ask, why does the –no-preserve-root option appear? This is probably mostly due to the UNIX philosophy of 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 files in the root directory?
Don’t tell me, there is such a need: for example, you want to clear all files in a chroot environment. We won't talk about chroot here. It uses a directory as a "prison". This directory logically forms a new "root directory". File operations in the prison cannot go outside the scope of the directory. Container technologies such as Docker and LXC/LXD that have become popular in recent years are all chroot technologies.
UEFI system
Okay, you may be more unique and want to clear all files in the root directory of the physical environment! But before you hit enter, think again, are you on a UEFI system?
Because the UEFI system will map its firmware, variables and settings to the /sys partition in the root directory, so if you clear everything in the root directory in the UEFI environment, /sys will also be cleared, which will be possible Will cause you to lose the estimated settings of UEFI, thus bricking the device.
sudo privilege escalation
In order to delete files belonging to systems such as root and other users, this command also needs to be preceded by sudo to elevate privileges.
After entering this command, you will be asked to enter a password. Whose password? Not the root password, but the password of the current user entering the command.
As for who can execute the sudo command and what commands he can execute through the sudo command, we will not go into details here. Please refer to our other articles. By the way, remember to distinguish the connection and difference between sudo and su commands.
Junk files
After studying this, we cannot forget the original intention of this command, which is to delete "junk files".
Are there junk files under Linux? have. These junk files generally come from:
Orphan files that are not managed by the package manager are left in the system after the software package is deleted
Useless dependent packages, after the software that requires these dependent packages are deleted, they are not deleted accordingly
Temporary files not cleaned
Legacy diagnostic files
Do these junk files need to be cleared? Generally speaking, most of these files on a Linux system will not have any impact on the healthy operation of the system, unless there are too many and occupy a lot of storage space and inodes.
So, if you feel that your Linux system is slow, it is almost certainly not due to junk files, at least at this point, the experience from Windows systems is not worth copying.
Through this article, we learned about the safe methods and steps to delete libraries and run files in Linux systems. Although this is a simple operation, performing it without the relevant knowledge can have serious consequences. Remember the importance of backing up your data, exercising caution, and restoring as needed.
The above is the detailed content of careful! How to safely delete libraries and run files in Linux systems. For more information, please follow other related articles on the PHP Chinese website!