linux下防止文件误删除的工具

linux系统下是没有回收站的,删除的文件或者目录默认是永远也找不回来的,因此,我们需要自己建立一个'回收站',来暂时存放删除的文件

#编辑文件
[root@hass-11 ~]# vim /etc/profile.d/trash.sh
#!/bin/bash
movetotrash(){
if [ "$1" != "" ];then
	\mv $@ /.trash/
else
	echo "command:rm filename ..."
fi
}

recoveryfile(){
if [ "$1" != "" ];then
	path=`pwd`
	cd /.trash/
	\mv -i $@ $path
	cd - >/dev/null 2>&1
else
	echo "command:ur filename ..."
fi
}

#创建目录
mkdir /.trash
#命令行执行
bash /etc/profile.sh/trash.sh
#在家目录下的.bashrc目录下添加以下内容
[root@hass-11 ~]# vim ~/.bashrc
...
alias rm=movetotrash
alias re=recoveryfile
alias rl="ls /.trash"
#命令行执行
[root@hass-11 ~]# source ~/.bashrc

#验证
[root@hass-11 ~]# ll
total 100
-rw-r--r-- 1 root root    48 Sep  2 14:00 c.txt
-rw-r--r-- 1 root root    48 Sep  2 14:53 d.txt
[root@hass-11 ~]# rm c.txt d.txt 
[root@hass-11 ~]# rl
c.txt  d.txt
[root@hass-11 ~]# re c.txt d.txt
[root@hass-11 ~]# ll
total 100
-rw-r--r-- 1 root root    48 Sep  2 14:00 c.txt
-rw-r--r-- 1 root root    48 Sep  2 14:53 d.txt

posted @ 2020-09-04 17:52  看萝卜在飘  阅读(203)  评论(0编辑  收藏  举报