Linux下建立类似windows的回收站

Linux人都知道rmrm -rf 的厉害,为了不小心删除问题,我做了一些小改变,适合Linux下有windows那样的回收站,一旦删除还可以找回被误删除的文件。如下操作。

我所在的操作系统的环境

root@ubuntu:~/test# cat /etc/issue
Ubuntu 14.04.2 LTS \n \l
root@ubuntu:~/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<<EOF
#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。其中\$@一样

3. 上面的作用,说白了就是命令rm 的重命名。


使用语法:

rm(删除),ur(撤销),rl(列出回收站),cleartrash(清空回收站)命令了。

 

#删除一个文件夹和文件都会被移动到回收站中。

$rm  filedirctory

 

#删除一个文件  

$rm  file.txt

 

#撤销对file.txt的删除

$ur  file.txt

 

#撤销filedirctory文件夹

$ur  filedirctory

 

#列出回收站

$rl

 

#清空回收站

cleartrash


-------------------------------------------------------------------------------------------

具体操作过程如下:

root@ubuntu:~/test# echo "1">1.txt
root@ubuntu:~/test# echo "2">2.txt
root@ubuntu:~/test# mkdir 123
root@ubuntu:~/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
root@ubuntu:~/test# mkdir -p ~/.Trash
root@ubuntu:~/test# ls ~/.Trash/


  1. 删除文件

root@ubuntu:~/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
root@ubuntu:~/test# rm 1.txt    #删除文件
root@ubuntu:~/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. 撤销删除文件

root@ubuntu:~/test# rl
1.txt
root@ubuntu:~/test# ls ~/.Trash/  
1.txt
root@ubuntu:~/test# ur 1.txt
root@ubuntu:~/test# ls ~/.Trash/
root@ubuntu:~/test# ls 
123  1.txt  2.txt
root@ubuntu:~/test# cat 1.txt 
1


3. 删除文件夹

root@ubuntu:~/test# 
root@ubuntu:~/test# ls
123  1.txt  2.txt
root@ubuntu:~/test# rm 123        #删除文件
root@ubuntu:~/test# rl 
123
root@ubuntu:~/test# ls ~/.Trash/  #删除的文件夹已经跑到回收站中了
123
root@ubuntu:~/test# ls
1.txt  2.txt


4. 撤销删除文件夹

root@ubuntu:~/test# rl
123
root@ubuntu:~/test# ls ~/.Trash/
123
root@ubuntu:~/test# ur 123
root@ubuntu:~/test# rl
root@ubuntu:~/test# ls
123  1.txt  2.txt


5. 清空回收站

root@ubuntu:~/test# ls
123  1.txt  2.txt 
root@ubuntu:~/test# rm *
root@ubuntu:~/test# ls
root@ubuntu:~/test# rl
123  1.txt  2.txt
root@ubuntu:~/test# cleartrash
Clear trash?[n]y
root@ubuntu:~/test# ls
root@ubuntu:~/test# rl


6. 此时若是恢复的时候没有加指定的参数问题

   此时就会吧垃圾回收站移动到当前文件夹内,且更名为.Trash

root@ubuntu:~/test# ur
root@ubuntu:~/test# ls
root@ubuntu:~/test# rl
ls: cannot access /root/.Trash: No such file or directory
root@ubuntu:~/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/
root@ubuntu:~/test# cd .Trash/
root@ubuntu:~/test/.Trash# ls
1.txt  2.txt  3.txt
root@ubuntu:~/test/.Trash# cd ..