Not long ago, because of one of my own hands, I executed "rm /*" and deleted the entire root directory by mistake. The lessons of data loss are sometimes huge and unbearable.
2. Prevent rm from being deleted by mistake
2.1 A dangerous command rm -rf
rm -rf
It is often used to delete files or directories, because when it is used, the files or directories that need to be deleted can be quickly deleted without repeated confirmation.
rm
Command, you can delete one or more files or directories in a directory, or delete a directory and all subdirectories and files under the directory. For linked files, only the entire linked file is deleted, not the original file.
-rf
parameter. -r
Or -R
, recursive processing, all files and subdirectories in the specified directory are processed together; it -f
means that files or directories are forced to be deleted.
2.2 Establish a recycle bin mechanism to prevent accidental deletion
After the recycle bin mechanism is established, when you delete data, you are not really deleting data, but moving files to a specific directory. Appears to be equivalent to the execution of rm
the command actually executes mv
the command.
2.2.1 Establishment process
To establish a recycle bin mechanism, you can replace the rm command by writing a shell script, or use the mv command to move the file to the recycle bin when you need to delete a file.
- In
/home/username/
a new directory in the directory, and named.trash
- In the
/home/username/tools/
directory, create a Shell script namedremove.sh
To
remove.sh
endow execute permissions:chmod +x /home/username/tools/remove.sh
remove.sh
The script content is as follows
# /home/username/tools/remove.sh
TRASH_DIR="/home/username/.trash"
for i in $*; do
STAMP=`date +%y%m%d-%H:%M:%S`
fileName=`basename $i`
mv $i $TRASH_DIR/$fileName.$STAMP
done
- Modify
~/.bashrc
, add the following line
alias rm="sh /home/username/tools/remove.sh"
To rm
set up an alias, using our own built remove.sh
alternative rm command
- Set
/etc/crontab
timer program to regularly empty the trash, for example,
0 0 * * * rm -rf /home/username/.trash/*
- Execution
source ~/.bashrc
to take effect immediately
delete the effect is as follows
note:
- If you want deleted and not directly into the recycle bin, it can be used directly
/bin/rm
to remove rm
Some of the previous parameters will be invalid, because the current one is actuallymv
- When using the new script to delete a file, if the deleted file has the same name, it will prompt whether to overwrite it.
2.3 Pay attention to the use of the command rm -rf variable
It should be avoided in the script as much as possible rm -rf $FOO/
, but recommended rm -rf $FOO
; delete as much as possible not to write relative paths, and do not bring variables
Reminder: I just fell here
2.4 Back up important data
Remote backup of important data is also a way to ensure data security.
2.5 rm-protection
2.5.1 Introduction to rm-protection
This is a fully compliant rm
data protection tools. If you install it, when executed rm
before command, each file or directory should check whether you want to delete is protected. By rm-protection
can set up a security question and answer to a file or directory commands.
The project address is: rm-protection
2.5.2 rm-protection installation
Directly through pip install rm-protection
, you can selectively set the alias as its rm-p
simplified command line.
By executing protection filename
, the file protection, when rm -rf filname
files are deleted, it will report the information to determine the questions and answers that really want to delete the data.
)