The Linux command replaces the rm command to prevent accidental deletion

  • 2020-05-15 02:35:14
  • OfStack

Recommended reading: Linux rm command mistakenly deleted files recovery method

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

2.. Under /home/username/tools/, create a new shell file and name it: remove.sh


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 by adding 1 line


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

Replace the rm command with the remove.sh we built ourselves

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


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

Empty the dustbin at 0 every day

5. source ~/.bashrc makes the substitution effective immediately

After the above steps, the files deleted by rm will be put into the dustbin. If you delete it by mistake, you can recover from it.

The above content gives you the Linux command to replace the rm command to prevent accidental deletion of the relevant knowledge, I hope to help you learn.


Related articles: