Home > Hosting > Server

How to prevent accidental deletion in Linux

2023-02-18 21:53:21

<p style="text-align: left;">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!</p><p style="text-align: left;">1. Create a new directory under the /home/username/ directory and name it: .trash</p><p style="text-align: left;">2 .. In the /home/username/tools/ directory, create a new shell file named: remove.sh</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">para_cnt=$# trash_dir=&quot;/home/username/.trash&quot; for i in $*; do stamp=`date +%s` filename=`basename $i` mv $i $trash_dir/$filename.$stamp done</p><p style="text-align: left;">3. Modify ~/.bashrc, add a line</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">alias rm=&quot;sh /home/username/tools/remove .sh&quot;</p><p style="text-align: left;">Replace the rm command with our self-built remove.sh</p><p style="text-align: left;">4. Set crontab to regularly empty the trash, such as:</p><p style="text-align: left;">Copy Code Code As follows:</p><p style="text-align: left;">0 0 * * * rm -rf /home/username/.trash/*</p><p style="text-align: left;">Empty the trash at 0:00 every day</p><p style="text-align: left;">5. source ~ /.bashrc makes the replacement take effect immediately</p><p style="text-align: left;">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.</p><p><br/></p>


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