为rm命令增加回收站功能

rm是个强大的命令,特别是rm -rf有时候强大到让你欲哭无泪,当你想清除当前目录下的所有文件和目录时,很简单

$sudo rm -rf ./*

 

这没什么,但是,但是如果不小心打成这样

$sudo rm -rf /*

 

兄弟,请节哀!

还 有其他各种各样的杯具,比如打开了很多窗口,有本地机器还有远程的几台服务器,本来想从这台机器执行rm -rf命令,却错误的输

入到了其他机器的终端窗口,总之rm太危险了,特别是带有-rf参数时一定要慎之又慎,但老虎也有打盹的时候啊,所以为什么不给 rm

一剂后悔药呢,嗯,就是它,trash-cli

 

trash-cli就是带有回收站(Trash)功能的命令行删除工具,其主要特点有

 

兼容rm命令行接口,可以alias rm为trash-cli

为删除的每一个文件记录原始路径,删除时间和文件访问权限

兼容GNOME和KDE桌面的trash,实现桌面和terminal操作的统一

实现了FreeDesktop.org Trash Specification

支持除home文件系统以外的其他文件系统,比如不同的分区或移动设备分区

安装trash-cli

 

Ubuntu和Debian仓库里面的版本太低了,而且有严重的bug,去官方网站下载最新版本的trash-cli,然后执行以下命令完成安装

 

1 $tar xvfz trash-cli-0.12.9.14.tar.gz

2 $cd trash-cli-0.12.9.14

3 $sudo python setup.py install

 

配置trash-cli替代rm

 

$vim ~/.bashrc

 

增添一行

alias rm="trash-put"

 

以后用rm删除文件的时候,文件会被移动到~/.local/share/Trash/files文件夹下,另一个文件夹~/.local/share/Trash/info下保

存了被删除文件的相关信息

 

trash-cli命令介绍

 

trash-put 删除文件

$trash-put foo

foo文件会被放入回收站(trashcan)

 

trash-list 列出回收站里面的文件

restore-trash 恢复指定的文件

1 $restore-trash

2 ...

3 ...

4 What file to restore [0..n]:

 

restore-trash会列出回收站里面的所有文件,每个文件前面有一个编号,从0开始,根据提示输入要恢复的文件的编号即可

 

trash-empty 清空回收站

trash-empty days 删除回收站里面超过指定天数的文件


转载:https://pypi.python.org/pypi/trash-cli/

trash-cli trashes files recording the original path, deletion date, and permissions. It uses the same trashcan used by KDE, GNOME, and XFCE, but you can invoke it from the command line (and scripts).

It provides these commands:

trash-put           trashes files and directories.
trash-empty         empty the trashcan(s).
trash-list          list trashed file.
restore-trash       restore a trashed file.

Usage

Trash a file:

$ trash-put foo

List trashed files:

$ trash-list
2008-06-01 10:30:48 /home/andrea/bar
2008-06-02 21:50:41 /home/andrea/bar
2008-06-23 21:50:49 /home/andrea/foo

Search for a file in the trashcan:

$ trash-list | grep foo
2007-08-30 12:36:00 /home/andrea/foo
2007-08-30 12:39:41 /home/andrea/foo

Restore a trashed file:

$ restore-trash
0 2007-08-30 12:36:00 /home/andrea/foo
1 2007-08-30 12:39:41 /home/andrea/bar
2 2007-08-30 12:39:41 /home/andrea/bar2
3 2007-08-30 12:39:41 /home/andrea/foo2
4 2007-08-30 12:39:41 /home/andrea/foo
What file to restore [0..4]: 4
$ ls foo
foo

Remove all files from the trashcan:

$ trash-empty

Remove only the files that have been deleted before <days> ago:

$ trash-empty <days>

Example:

$ date
Tue Feb 19 20:26:52 CET 2008
$ trash-list
2008-02-19 20:11:34 /home/einar/today
2008-02-18 20:11:34 /home/einar/yesterday
2008-02-10 20:11:34 /home/einar/last_week
$ trash-empty 7
$ trash-list
2008-02-19 20:11:34 /home/einar/today
2008-02-18 20:11:34 /home/einar/yesterday
$ trash-empty 1
$ trash-list
2008-02-19 20:11:34 /home/einar/today

Using it as 'rm' alias

trash-put accept all the options that GNU rm does, if you prefer (I don't) you can set up this alias in your .bashrc:

alias rm='trash-put'

At the present the semantic of trash-put is sligthly different from the one of rm, for example, while rm requires -R for deleting directories trash-put does not. This may change in future.

Keep in mind that Bash aliases are used only in interactive shells, so using this alias should not interfere with scripts that expects to use rm.

Installation (the easy way)

Requirements:

  • Python 2.7
  • setuptools (use apt-get install python-setuptools on Debian)

Installation command:

easy_install trash-cli

Installation from sources

# grab the latest sources
wget https://github.com/andreafrancia/trash-cli/tarball/master

# unpack and install
tar xfz andreafrancia-trash-cli-xxxxxxxx.tar.gz
cd andreafrancia-trash-cli-xxxxxxxx
sudo python setup.py install

Bugs and feedback

If you discover a bug please report it to:

https://github.com/andreafrancia/trash-cli/issues

You can reach me via email at me@andreafrancia.it . For twitter use @andreafrancia or #trashcli

Development

Environment setup:

virtualenv env --no-site-packages
source env/bin/activate
pip install -r requirements-dev.txt

Running tests:

nosetests unit_tests           # run only unit tests
nosetests integration_tests    # run all integration tests
nosetests -A 'not stress_test' # run all tests but stress tests
nosetests                      # run all tests

Check the installation process before release:

python check_release_installation.py

Profiling unit tests:

pip install gprof2dot
nosetests --with-profile --profile-stats-file stats.pf --profile-restrict=unit_tests unit_tests
gprof2dot -w  -f pstats stats.pf | dot -Tsvg >| stats.svg
open stats.svg
原文地址:https://www.cnblogs.com/pengdonglin137/p/3625006.html