How to prevent accidental deletion in Linux

02-18-2023

This article mainly explains how Linux prevents accidental deletion. Interested friends may wish to take a look. The method described in this article is simple, quick and practical. Let the editor take you to learn how to prevent accidental deletion in Linux!

1. Create a new directory under the /home/username/ directory and name it: .trash

2 .. In the /home/username/tools/ directory, create a new shell file named: remove.sh

Copy code The code is as follows:

para_cnt=$# trash_dir="/home/username/.trash" for i in $*; do stamp=`date +%s` filename=`basename $i` mv $i $trash_dir/$filename.$stamp done

3. Modify ~/.bashrc, add a line

Copy code The code is as follows:

alias rm="sh /home/username/tools/remove .sh"

Replace the rm command with our self-built remove.sh

4. Set crontab to regularly empty the trash, such as:

Copy Code Code As follows:

0 0 * * * rm -rf /home/username/.trash/*

Empty the trash at 0:00 every day

5. source ~ /.bashrc makes the replacement take effect immediately

After the above steps, the files deleted by rm will be put into the trash can. If deleted by mistake, it can be restored from it.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us