Linux prevents accidental deletion

02-09-2024

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

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

Copy the code as follows:

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

3. Modify~/. bashrc and add a new line

Copy the code as follows:

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

Replace the rm command with our self built remove.sh

4. Set up crontab and regularly empty the trash can, such as:

Copy the code as follows:

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

Empty the trash can at 0:00 every day

5. Source~/. bashrc makes the replacement effective immediately

After the above steps, the files deleted by RM will be placed in the trash can. If accidentally deleted, 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