【转载】trash-cli使用说明

来源:互联网 发布:java网上商城系统 编辑:程序博客网 时间:2024/05/17 03:47

转自老高的博客
trash-cli命令的意思是命令行下的回收站了,我们通常是可以在linux内核系统安装它,之后删除文件就在回收站了,下面一起来了解一下。

Trash-cli 提供一套命令行下的回收站工具。在 Gnome 环境下,这个命令行下的回收站和 Gnome 回收站是统一的,无论是在命令行下还是在图形环境下,删除的文件都会进入这个回收站。

玩过Linux的同学应该都知道这个要命的命令,此命令一出,系统几乎废了。
rm -rf /
虽然一再强调rm的时候要注意,但是还是会有同学犯错误删文件,有什么办法能够减少此类事情的发生呢?
今天老高就要介绍此工具—– trash-cli。他实现了类似回收站的功能,以后删除的时候就有了后悔药。
trash-cli
trash-cli的项目地址 trash-cli
I. 安装
trash-cli的安装很简单
cd /tmp
https://github.com/andreafrancia/trash-cli
cd trash-cli
sudo python setup.py install
安装成功后我们的系统就有了以下工具
➜ ~ ll /usr/bin/|grep trash
-rwxr-xr-x 1 root root 123 5月 28 19:38 trash
-rwxr-xr-x 1 root root 125 5月 28 19:38 trash-empty
-rwxr-xr-x 1 root root 124 5月 28 19:38 trash-list
-rwxr-xr-x 1 root root 123 5月 28 19:38 trash-put
-rwxr-xr-x 1 root root 127 5月 28 19:38 trash-restore
-rwxr-xr-x 1 root root 122 5月 28 19:38 trash-rm
功能说明
trash == trash-put == 删除
trash-empty 清空
trash-list 列出回收站
trash-restore 恢复文件
trash-rm 删除回收站中指定文件
II. 如何使用
替换命令
为了安全着想,我们先将rm命令替换为trash
vim /etc/bashrc
添加以下内容,替换系统删除命令。
根据作者的README文件,建议还是轻易不要把rm做替换,下面的替换作废。
alias log=’cd /var/log’ # 安全删除 alias rm=’trash’ # 列出回收站 alias rl=’trash-list’
作者原文
Can I alias rm to trash-put?
You can but you shouldn’t. In the early days I thought it was good idea do that but now I changed my mind.
The interface of trash-put seems to be compatible with rm it has a different semantic that will cause you problems. For example, while rm requires -R for deleting directories trash-put does not.
But sometimes I forgot to use trash-put, really can’t I?
You may alias rm to something that will remind you to not use it:
alias rm=’echo “This is not the command you are looking for.”; false’
If you really want use rm simply prepend a slash:
\rm file-without-hope
Note that Bash aliases are used only in interactive shells, so using this alias should not interfere with scripts that expects to use rm.
所以新的替换是

使用rm前确认

alias rm=’echo ” This is not the command you are looking for.If you really want use rm simply prepend a slash”; false’
以后执行rm的话,就会出现以下情况
➜ ~ rm
This is not the command you are looking for.If you really want use rm simply prepend a slash
以后如果真的要删除的话,请使用\rm 真的很没用的文件,而使用trash 需要删除的文件。
完成后输入source /etc/bashrc是修改的别名生效。
常用操作

安全删除

rm ./*

查看回收站

➜ /tmp rl
2015-05-28 19:59:54 /tmp/redis-stable

清空回收站

trash-empty

清空10天以前放入回收站的文件

trash-empty 10

还原文件

➜ /tmp trash-restore
0 2015-05-28 19:59:54 /tmp/redis-stable
What file to restore [0..0]: 输入对应序号还原
例子
在使用命令行操作电脑的时候,有时对 rm -rf 使用不慎会删去重要资料。好在命令行下有回收站可以用 —— trash-cli.
Trash-cli 提供一套命令行下的回收站工具。在 Gnome 环境下,这个命令行下的回收站和 Gnome 回收站是统一的,无论是在命令行下还是在图形环境下,删除的文件都会进入这个回收站。
Trash-cli 可以通过软件源安装。以我使用的 Ubuntu 为例:
sudo apt-get install trash-cli
安装完成后,我们可以对命令行换镜稍做修改,让 rm 命令直接把文件移入回收站,这样操作更加顺手。以我使用的 bash 为例,修改 $HOME/.bashrc 文件,加入下面的行:
alias rm!=”/bin/rm -vi”
alias rm=trash
alias lstrash=list-trash
这样,rm 命令会把文件移入回收站,而 rm! 命令则会直接删除文件。list-trash 命令需要输入六个字母再按 Tab 键才能补全,因此改为 lstrash.
除了 trash 和 list-trash, trash-cli 提供的其他命令有 empty-trash 和 restore trash. 下面是使用 trash-cli 的情形:

sun: ~ $ list-trash

一开始回收站为空

sun: ~ touchtestsun:  trash test
sun: ~ $ list-trash
2012-07-13 17:31:46 /home/sun/test

list-trash 会显示回收站中的文件的删除时间和初始位置

sun: ~ touchtesttest1test2test3sun:  trash test test1 test2 test3

可以一次移动多个文件到回收站,用法和 rm 类似

sun: ~ listtrash2012071317:31:46/home/sun/test2012071317:32:12/home/sun/test32012071317:32:12/home/sun/test12012071317:32:12/home/sun/test2012071317:32:12/home/sun/test2sun:  cd testdir/
sun: ~/testdir touchtest4sun: /testdir trash test4
sun: ~/testdir $ restore-trash test4
0 2012-07-13 17:32:56 /home/sun/testdir/test4
What file to restore [0..0]: 0

在用 restore-trash 时,trash-cli 会寻找名称匹配的所有文件,让用户选择恢复哪一个

sun: ~/testdir listtrash2012071317:31:46/home/sun/test2012071317:32:12/home/sun/test32012071317:32:12/home/sun/test12012071317:32:12/home/sun/test2012071317:32:12/home/sun/test2sun: /testdir ls
test4

文件已经恢复

0 0
原创粉丝点击