你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下建立类似Windows的回收站

[日期:2017-03-02] 来源:Linux社区  作者:caimengzhi [字体: ]

Linux人都知道rm、rm -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 ..

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-03/141263.htm

linux
相关资讯       Linux回收站 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款