Package: 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           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.UsageTrash a file:$ trash-put fooList 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/fooSearch 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/fooRestore 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
fooRestore a trashed file while overwriting existing files:$ echo "original">foo
$ ls
foo
$ trash 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
originalRestore 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 foo2Remove all files from the trashcan:$ trash-emptyRemove only the files that have been deleted more than <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/todayRemove only files matching a pattern:$ trash-rm \*.oNote: you need to use quotes in order to protect the pattern from shell expansion.FAQHow to create a top level .Trash dir?Stepssudo mkdir --parent /.Trash
sudo chmod a+rw /.Trash
sudo chmod +t /.TrashCan I aliasrmtotrash-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 oftrash-putseems to be compatible withrm, it has
different semantics which will cause you problems. For example, whilermrequires-Rfor deleting directoriestrash-putdoes not.But sometimes I forget to usetrash-put, really can’t I?You could aliasrmto 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 userm, simply prepend a backslash to bypass the
alias:\rm file-without-hopeNote that Bash aliases are used only in interactive shells, so using
this alias should not interfere with scripts that expect to userm.Where the trashed files go?File trashed from the home partition will be moved here:~/.local/share/Trash/How to auto delete files older that 30 days?Run this:(crontab -l ; echo "@daily $(which trash-empty) 30") | crontab -This will update your crontab file with atrash-emptycommand that runs daily
and removes files older than 30 days. To review your crontab use:crontab -lInstallationThe easy wayRequirements:Python 3 (Python 2.7 also work)pipx(optional, to install in a clean environment)If pipx is available:pipx install trash-cliAlternatively, install with vanilla pip:pip install trash-cliNote: you may want add ~/.local/bin to the PATH:echo 'export PATH="$PATH":~/.local/bin' >> ~/.bashrc
source ~/.bashrc # reload .bashrcFor uninstalling use:pipx uninstall trash-clior:pip uninstall trash-cliBleeding 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 distributionThen install the latest version from git:$ [sudo] pip install git+https://github.com/andreafrancia/trash-cliAfter the user installation you may want add this line to your .bashrc/.zshrc:export PATH=~/.local/bin:"$PATH"From package managerDebian/Ubuntu (apt):sudo apt install trash-cliArch Linux (pacman):sudo pacman -S trash-cliFedora (dnf):sudo dnf install trash-cliInstall shell completionsYou need 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
doneBugsIf you discover a bug please report it here:https://github.com/andreafrancia/trash-cli/issuesFeedbackYou can send me an email usingandrea@andreafrancia.it.DevelopmentEnvironment setup:virtualenv env --no-site-packages
source env/bin/activate
pip install -r requirements-dev.txt -r requirements.txtRunning tests:pytest -m 'not slow'        # run only fast tests
pytest -m 'slow'            # run slow tests
pytest                      # run all testsThanksThanks to Paypal donors.Thanks toproject contributors.Thanks toJetBrainsfor their license for Open Source Development