One point a day (2) to prevent rm from being deleted by mistake

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.

rmCommand, 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.

-rfparameter. -rOr -R, recursive processing, all files and subdirectories in the specified directory are processed together; it -fmeans 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 rmthe command actually executes mvthe 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.shendow execute permissions:chmod +x /home/username/tools/remove.sh
    .trash

  • remove.shThe 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 rmset up an alias, using our own built remove.shalternative rm command

  • Set /etc/crontabtimer program to regularly empty the trash, for example,
0 0 * * * rm -rf /home/username/.trash/*
  • Execution source ~/.bashrcto take effect immediately
    delete the effect is as follows
    rm

note:

  • If you want deleted and not directly into the recycle bin, it can be used directly /bin/rmto remove
  • rm Some of the previous parameters will be invalid, because the current one is actually mv
  • 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 rmdata protection tools. If you install it, when executed rmbefore command, each file or directory should check whether you want to delete is protected. By rm-protectioncan 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-psimplified command line.
By executing protection filename, the file protection, when rm -rf filnamefiles are deleted, it will report the information to determine the questions and answers that really want to delete the data.
rp-protection)

Guess you like

Origin blog.csdn.net/qq_36148847/article/details/82080697