
trash-cli
CLI把文件移进回收站而不是直接删除,可以列出、恢复、清空已删文件,agent 误删的内容还能找回。遵循 FreeDesktop.org 回收站规范,与 GNOME、KDE 共用同一个回收站。
trash-cli - Command Line Interface to FreeDesktop.org Trash
|Downloads|
简体中文_
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 trash files and directories.
trash-empty empty the trashcan(s).
trash-list list trashed files.
trash-restore restore a trashed file.
trash-rm remove individual files from the trashcan.
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::
$ trash-restore
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
Restore a trashed file while overwriting existing files::
$ echo "original">foo
$ ls
foo
$ trash-put foo
$ echo "new">foo
$ trash-restore --overwrite
0 2022-11-01 22:15:00 /home/andrea/foo
What file to restore [0..0]: 0
$ cat foo
original
Restore multiple trashed files separated by ',', also support range::
$ trash-restore
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
What file to restore [0..3]: 0-2, 3
$ ls foo bar bar2 foo2
foo bar bar2 foo2
Remove all files from the trashcan::
$ trash-empty
Remove all files from the trashcan (force/silent - no prompt)::
$ trash-empty -f
Remove only the files that have been deleted more than 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
Remove only files matching a pattern::
$ trash-rm \*.o
Note: you need to use quotes in order to protect the pattern from shell expansion.
FAQ
How to create a top level .Trash dir?
Steps ::
sudo mkdir --parent /.Trash
sudo chmod a+rw /.Trash
sudo chmod +t /.Trash
Can I alias `rm` to `trash-put`?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can but you shouldn't. In the early days I thought it was a good idea to do
that but now I changed my mind.
Although the interface of `trash-put` seems to be compatible with `rm`, it has
different semantics which will cause you problems. For example, while `rm`
requires `-R` for deleting directories `trash-put` does not.
But sometimes I forget to use `trash-put`, really can't I?
You could 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'
Then, if you really want to use rm, simply prepend a backslash to bypass the
alias::
\rm file-without-hope
Note that Bash aliases are used only in interactive shells, so using
this alias should not interfere with scripts that expect to use rm.
Where do the trashed files go?
File trashed from the home partition will be moved here::
~/.local/share/Trash/
How to auto delete files older than 30 days?
Run this::
(crontab -l ; echo "@daily $(which trash-empty) 30") | crontab -
This will update your crontab file with a trash-empty command that runs daily
and removes files older than 30 days. To review your crontab use: crontab -l
Installation
The easy way
Requirements:
* Python 3 (Python 2.7 also works)
* pipx_ (optional, to install in a clean environment)
If pipx is available::
pipx install trash-cli
Alternatively, install with vanilla pip::
pip install trash-cli
Note: you may want to add ~/.local/bin to the PATH::
echo 'export PATH="$PATH":~/.local/bin' >> ~/.bashrc
source ~/.bashrc # reload .bashrc
For uninstalling use::
pipx uninstall trash-cli
or::
pip uninstall trash-cli
Bleeding Edge (from sources)
First of all you need to uninstall any previous version of trash-cli::
$ [sudo] pip uninstall trash-cli # remove the previous version (with pip)
$ [sudo] apt-get remove trash-cli # remove the previous version (with apt)
$ [sudo] yum uninstall trash-cli # remove the previous version (with yum)
$ ... # refer to the package manager of your distribution
Then install the latest version from git::
$ [sudo] pip install git+https://github.com/andreafrancia/trash-cli
After the user installation you may want to add this line to your .bashrc/.zshrc::
export PATH=~/.local/bin:"$PATH"
From package manager
Debian/Ubuntu (apt)::
sudo apt install trash-cli
Arch Linux (pacman)::
sudo pacman -S trash-cli
Fedora (dnf)::
sudo dnf install trash-cli
MacOS (Homebrew)::
brew install trash-cli
echo 'export PATH="$(brew --prefix trash-cli)/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Install shell completions
You need to install by::
pipx install 'trash-cli[completion]'
or::
pip install 'trash-cli[completion]'
Then::
cmds=(trash-empty trash-list trash-restore trash-put trash)
for cmd in ${cmds[@]}; do
$cmd --print-completion bash | sudo tee /usr/share/bash-completion/completions/$cmd
$cmd --print-completion zsh | sudo tee /usr/share/zsh/site-functions/_$cmd
$cmd --print-completion tcsh | sudo tee /etc/profile.d/$cmd.completion.csh
done
Missing support for Btrfs volumes
trash-cli does not support Btrfs volumes. I don't have any system nor time and/or knowledge to implement this kind of support.
If you need a trash implementation, you can check out the rmw_ project.
.. _rmw: https://github.com/theimpossibleastronaut/rmw
If you want to, and can, and know how to add support for Btrfs with sensible automation tests, please send a pull request.
Bugs
If you discover a bug please report it here:
https://github.com/andreafrancia/trash-cli/issues
Feedback
You can send me an email using andrea@andreafrancia.it.
Development
Environment setup::
python -m venv .venv
source .venv/bin/activate
pip install . '.[dev]'
Running tests::
pytest -m 'not slow' # run only fast tests
pytest -m 'slow' # run slow tests
pytest # run all tests
Thanks
Thanks to Paypal donors.
Thanks to project contributors_.
Thanks to JetBrains_ for their license for Open Source Development
.. |Downloads| image:: https://img.shields.io/pypi/dm/trash-cli .. _简体中文: https://github.com/andreafrancia/trash-cli/blob/master/README_zh-CN.rst .. _project contributors: https://github.com/andreafrancia/trash-cli/graphs/contributors .. _JetBrains: https://jb.gg/OpenSource .. _pipx: https://pypa.github.io/pipx/
相关资源

Hermes Agent
CLINous Research 出品的自我改进型 Agent:内置记忆与从经验中自动生成技能的学习循环,可接任意模型供应商;hermes 命令提供终端界面,也能经网关接入 Telegram、Slack 等渠道并定时执行任务。
NousResearch/hermes-agent254k
fzf
CLI通用命令行模糊查找器,可对文件列表、命令历史、进程等任意文本输入做模糊筛选;除交互界面外还有 --filter 非交互过滤模式,并提供 Shell 快捷键与补全集成。
junegunn/fzf83.6k
Homebrew
CLImacOS 与 Linux 上的包管理器:agent 可以用 brew install 安装命令行工具和桌面应用(cask),也可以用 brew bundle 按 Brewfile 批量还原开发环境。
Homebrew/brew50k
fd
CLI简单快速的 find 替代品:语法直观,支持正则与 glob 匹配,默认忽略隐藏文件和 .gitignore 中的路径,还能对搜索结果并行执行命令,适合 agent 快速定位文件。
sharkdp/fd44.8k
croc
CLI一条命令在任意两台电脑之间收发文件和文件夹:端到端加密(PAKE),点对点直连、不通时走中继,支持断点续传与代理,不需要自建服务器或端口转发。
schollz/croc40.6k
zoxide
CLI会记住常用目录的智能 cd 命令,按访问频率与最近使用时间排序,输入目录名片段即可跳转;安装后需在 Shell 配置中加入 zoxide init 完成初始化。
ajeetdsouza/zoxide40.1k