1.前言
曾几何时,一条 rm -rf/useless /* 命令干掉了我们需要的文件,也干掉的整个系统,
这是是不是与哭无泪啊。
像这种使用 rm 命令误删除文件的情况,我们会注意,但是我们不能保证百分百的不会出现这种情况,毕竟事情总有偶然,那么对于这种情况我们是不是就束缚无策呢,不!我们有相应的解决办法,那就是使用 trash-cli。
1.1 trash-cli介绍
Trash-cli 提供一套命令行下的回收站工具。在 Gnome 环境下,这个命令行下的回收站和 Gnome 回收站是统一的,无论是在命令行下还是在图形环境下,删除的文件都会进入这个回收站。
1.2 下载trash-cli工具包
在github上提供了trash-cli的zip包下载链接:https://github.com/andreafrancia/trash-cli
1.3 安装trash-cli工具
#安装环境
[[email protected] ~]# uname -r 2.6.32-504.el6.x86_64 [[email protected] ~]# uname -m x86_64 [[email protected] ~]# cat /etc/redhat-release CentOS release 6.6 (Final)
#解压安装trash-cli
[[email protected] tools]# ls trash-cli-master.zip [[email protected] tools]# unziptrash-cli-master.zip [[email protected] tools]# cd trash-cli-master [[email protected] trash-cli-master]# python setup.pyinstall
1.4 trash-cli管理命令说明
#trash-cli安装完成后会生成如下几个命令
[[email protected] trash-cli-master]# ls -l/usr/bin/|grep trash -rwxr-xr-x 1 root root 123 Mar 2 19:34 trash -rwxr-xr-x 1 root root 125 Mar 2 19:34 trash-empty -rwxr-xr-x 1 root root 124 Mar 2 19:34 trash-list -rwxr-xr-x 1 root root 123 Mar 2 19:34 trash-put -rwxr-xr-x 1 root root 127 Mar 2 19:34 trash-restore -rwxr-xr-x 1 root root 122 Mar 2 19:34 trash-rm
功能说明:
trash-put //将文件或目录移入回收站
trash-empty days //清空回收站,trash-empty 10 表示清空十天以前的文件
trash-list //列出回收站中的文件
trash-restore //还原回收站中的文件
trash-rm // 删除回首站中的单个文件
#特别提示:trash-put命令会把我们想要删除的文件移动到 ~/.local/share/Trash/files 中当我们使用trash-restore不能恢复时可以从~/.local/share/Trash/files目录中将需要恢复的文件或者目录cp到相应路径,相关信息记录在~/.local/share/Trash/info中。
1.5 用trash-cli替换rm命令
#为了系统安全着想我们将rm命令替换为trash-put。防止误删除操作。
[[email protected] ~]# echo ‘aliasrm="trash-put"‘ >>/etc/profile [[email protected] ~]# tail -1 /etc/profile alias rm="trash-put" [[email protected] ~]# . /etc/profile
1.6 trash-cli工具使用示例
示例1:删除/etc/issue文件,并恢复。
[[email protected] ~]# cat /etc/issue CentOS release 6.6 (Final) Kernel \r on an \m [[email protected] ~]# rm /etc/issue [[email protected] ~]# cat /etc/issue cat: /etc/issue: No such file ordirectory [[email protected] ~]# cp~/.local/share/Trash/files/issue /etc/ [[email protected] ~]# cat /etc/issue CentOS release 6.6 (Final) Kernel \r on an \m
示例2:误操作执行了rm -rf /* 恢复
#当我们执行了rm -rf /* 操作即使立刻Ctrl+c终止操作也会删除一部分内容
[[email protected] ~]# rm -rf /* trash-put: cannot trash directory`/boot‘ ^CTraceback (most recent call last): File "/usr/bin/trash-put", line 5, in <module> sys.exit(main()) File "/usr/lib/python2.6/site-packages/trashcli/put.py", line17, in main ).run(sys.argv) File "/usr/lib/python2.6/site-packages/trashcli/put.py", line46, in run self.trash_all(args) File "/usr/lib/python2.6/site-packages/trashcli/put.py", line52, in trash_all self.trashcan.trash(arg) File "/usr/lib/python2.6/site-packages/trashcli/put.py", line248, in trash trashed_file = trash_dir.trash(file) File "/usr/lib/python2.6/site-packages/trashcli/put.py", line372, in trash self.move(path, where_to_store_trashed_file) File "/usr/lib/python2.6/site-packages/trashcli/fs.py", line47, in move return shutil.move(path, str(dest)) File "/usr/lib64/python2.6/shutil.py", line 257, in move copytree(src, real_dst, symlinks=True) File "/usr/lib64/python2.6/shutil.py", line 156, in copytree copy2(srcname, dstname) File "/usr/lib64/python2.6/shutil.py", line 95, in copy2 copyfile(src, dst) File "/usr/lib64/python2.6/shutil.py", line 52, in copyfile
copyfileobj(fsrc, fdst) File "/usr/lib64/python2.6/shutil.py", line 31, in copyfileobj fdst.write(buf) KeyboardInterrupt
#终止删除后发现ls,cp等命令已经都不能执行了
[[email protected] ~]# ls -bash: /bin/ls: No such file or directory [[email protected] ~]# cp -bash: /bin/cp: No such file ordirectory
#切换到回收站的文件夹下也无法查看其中内容了
[[email protected] ~]# cd~/.local/share/Trash/files/ [[email protected] files]# ls -bash: /bin/ls: No such file ordirectory
#使用rsync命令将其误删除的文件恢复到 /目录下
[[email protected] ~]# cd~/.local/share/Trash/files/ [[email protected] files]# rsync -avzP ./* / sending incremental file list bin/ bin/alsaunmute 123 100% 0.00kB/s 0:00:00 (xfer#1, to-check=151/155) bin/arch 27776 100% 2.65MB/s 0:00:00 (xfer#2, to-check=150/155) bin/awk -> gawk bin/basename 26264 100% 2.09MB/s 0:00:00 (xfer#3, to-check=148/155) bin/bash 940416 100% 7.60MB/s 0:00:00 (xfer#4, to-check=147/155)
#恢复完成后再次ls查看命令以恢复,跟目录下目录页恢复了
[[email protected] files]# ls bin boot dev [[email protected] files]# cd / [[email protected] /]# ls bin dev home lib64 media opt root selinux srv tmp var boot etc lib lost+found mnt proc sbin server sys usr
1.7 清空回收站内文件
#清空所有
[[email protected] ~]# trash-empty
#清空7天以前的
[[email protected] ~]# trash-empty 7