linux rm删除文件之后,恢复就比较麻烦了,即使恢复了,文件名格式都变成一串数字了。
vi ~/.bashrc
注释第5行的别名
#alias rm='rm -i'
mkdir -p ~/.trash
alias rm=trash
alias r=trash
alias rl='ls ~/.trash'
alias ur=undelfile
undelfile()
{
mv -i ~/.trash/$@ ./
}
trash()
{
mv $@ ~/.trash/
}
cleartrash()
{
read -p "clear sure?[n]" confirm
[ $confirm == 'y' ] || [ $confirm == 'Y' ] && /bin/rm -rf ~/.trash/*
}
重新加载环境变量
source ~/.bashrc
使用命令ll -a查看目录,发现多了目录.trash,这个目录是用来存在删除的文件
drwxr-xr-x. 2 root root 4096 Jun 4 11:31 .trash
[root@localhost ~]#rm percona-xtrabackup_2.2.3.orig.tar.gz
[root@localhost ~]#ll .trash/
total 33780
-rw-r--r--. 1 root root 34584359 Jun 2 09:39 percona-xtrabackup_2.2.3.orig.tar.gz
如果需要清空回收站文件
使用以下命令
[root@localhost ~]#cleartrash
clear sure?[n]y
再次查看,发现空了。
[root@localhost ~]#ll .trash/
total 0
比如/bin/rm 1.txt
它是不会保存到.trash目录的。
可以写一个脚本
#!/bin/bash
find /root/.trash -ctime 7 -type f -name "*" -exec /bin/rm {} \;
如果Linux除了root用户,还有其他用户需要登陆服务器,也想他们使用回收站机制
可以修改系统环境变量
vi /etc/profile
最后一行添加
mkdir -p ~/.trash
alias rm=trash
alias r=trash
alias rl='ls ~/.trash'
alias ur=undelfile
undelfile()
{
mv -i ~/.trash/$@ ./
}
trash()
{
mv $@ ~/.trash/
}
cleartrash()
{
read -p "clear sure?[n]" confirm
[ $confirm == 'y' ] || [ $confirm == 'Y' ] && /bin/rm -rf ~/.trash/*
}
重新加载环境变量
source /etc/profile
创建普通用户测试
useradd a
passwd a
登陆Linux
查看目录,发现会创建.trash目录
[a@localhost ~]$ll -a
total 24
drwx------. 3 a a 4096 Jun 4 11:45 .
drwxr-xr-x. 5 root root 4096 Jun 4 11:44 ..
-rw-r--r--. 1 a a 18 Oct 16 2014 .bash_logout
-rw-r--r--. 1 a a 176 Oct 16 2014 .bash_profile
-rw-r--r--. 1 a a 124 Oct 16 2014 .bashrc
drwxrwxr-x. 2 a a 4096 Jun 4 11:45 .trash
[a@localhost ~]$touch 1.txt
[a@localhost ~]$rm 1.txt
[a@localhost ~]$ll .trash/
total 0
-rw-rw-r--. 1 a a 0 Jun 4 11:45 1.txt
如果对.trash目录位置觉得不爽,可以修改环境变量,改成其他位置,注意保证目录可写。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。