Linux下建立类似windows的回收站
玩Linux人都知道rm、rm -rf的厉害,为了不小心删除问题,我做了一些小改变,适合Linux下有windows那样的回收站,一旦删除还可以找回被误删除的文件。如下操作。
我所在的操作系统的环境[email protected]:~/test# cat /etc/issue
Ubuntu 14.04.2 LTS \n \l
[email protected]:~/test# uname -a
Linux ubuntu 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
快速执行mkdir -p ~/.Trash
cat >>~/.bashrc<
#add by caimengzhi at $(date +%F) for Linux trash start
alias rm=trash
alias rl=‘ls ~/.Trash‘
alias ur=undelfile
undelfile()
{
mv -i ~/.Trash/\$@ ./
}
trash()
{
mv \$@ ~/.Trash/
}
cleartrash()
{
read -p "Clear trash?[n]" confirm
[ \$confirm == ‘y‘ ] || [ \$confirm == ‘Y‘ ] && /usr/bin/rm -rf ~/.Trash/*
}
#add by caimengzhi at $(date +%F) for Linux trash end
EOF
source ~/.bashrc
说明:
1. ~/.Trash就是以后被删除的文件和文件夹移动到的地方,也就是回收站
2. \$confirm 是实现脱意思,也就是最后在文件中就是$confirm。其中\[email protected]
3. 上面的作用,说白了就是命令rm 的重命名。
使用语法:
rm(删除),ur(撤销),rl(列出回收站),cleartrash(清空回收站)命令了。
#删除一个文件夹和文件都会被移动到回收站中。
$rm filedirctory
#删除一个文件
$rm file.txt
#撤销对file.txt的删除
$ur file.txt
#撤销filedirctory文件夹
$ur filedirctory
#列出回收站
$rl
#清空回收站
cleartrash
-------------------------------------------------------------------------------------------
具体操作过程如下:[email protected]:~/test# echo "1">1.txt
[email protected]:~/test# echo "2">2.txt
[email protected]:~/test# mkdir 123
[email protected]:~/test# ll
total 20
drwxr-xr-x 3 root root 4096 Jan 16 14:07 ./
drwx------ 6 root root 4096 Jan 16 14:05 ../
drwxr-xr-x 2 root root 4096 Jan 16 14:07 123/
-rw-r--r-- 1 root root 2 Jan 16 14:07 1.txt
-rw-r--r-- 1 root root 2 Jan 16 14:07 2.txt
[email protected]:~/test# mkdir -p ~/.Trash
[email protected]:~/test# ls ~/.Trash/
删除文件[email protected]:~/test# ll
total 20
drwxr-xr-x 3 root root 4096 Jan 16 14:07 ./
drwx------ 6 root root 4096 Jan 16 14:05 ../
drwxr-xr-x 2 root root 4096 Jan 16 14:07 123/
-rw-r--r-- 1 root root 2 Jan 16 14:07 1.txt
-rw-r--r-- 1 root root 2 Jan 16 14:07 2.txt
[email protected]:~/test# rm 1.txt #删除文件
[email protected]:~/test# ll
total 16
drwxr-xr-x 3 root root 4096 Jan 16 14:09 ./
drwx------ 6 root root 4096 Jan 16 14:05 ../
drwxr-xr-x 2 root root 4096 Jan 16 14:07 123/
-rw-r--r-- 1 root root 2 Jan 16 14:07 2.txt
2. 撤销删除文件[email protected]:~/test# rl
1.txt
[email protected]:~/test# ls ~/.Trash/
1.txt
[email protected]:~/test# ur 1.txt
[email protected]:~/test# ls ~/.Trash/
[email protected]:~/test# ls
123 1.txt 2.txt
[email protected]:~/test# cat 1.txt
1
3. 删除文件夹[email protected]:~/test#
[email protected]:~/test# ls
123 1.txt 2.txt
[email protected]:~/test# rm 123 #删除文件
[email protected]:~/test# rl
123
[email protected]:~/test# ls ~/.Trash/ #删除的文件夹已经跑到回收站中了
123
[email protected]:~/test# ls
1.txt 2.txt
4. 撤销删除文件夹[email protected]:~/test# rl
123
[email protected]:~/test# ls ~/.Trash/
123
[email protected]:~/test# ur 123
[email protected]:~/test# rl
[email protected]:~/test# ls
123 1.txt 2.txt
5. 清空回收站[email protected]:~/test# ls
123 1.txt 2.txt
[email protected]:~/test# rm *
[email protected]:~/test# ls
[email protected]:~/test# rl
123 1.txt 2.txt
[email protected]:~/test# cleartrash
Clear trash?[n]y
[email protected]:~/test# ls
[email protected]:~/test# rl
6. 此时若是恢复的时候没有加指定的参数问题
此时就会吧垃圾回收站移动到当前文件夹内,且更名为.Trash[email protected]:~/test# ur
[email protected]:~/test# ls
[email protected]:~/test# rl
ls: cannot access /root/.Trash: No such file or directory
[email protected]:~/test# ll
total 12
drwxr-xr-x 3 root root 4096 Jan 16 14:29 ./
drwx------ 5 root root 4096 Jan 16 14:29 ../
drwxr-xr-x 3 root root 4096 Jan 16 14:29 .Trash/
[email protected]:~/test# cd .Trash/
[email protected]:~/test/.Trash# ls
1.txt 2.txt 3.txt
[email protected]:~/test/.Trash# cd ..
原文:http://caimengzhi.blog.51cto.com/9787265/1892262