


























There is no trash can for the Linux CLI. rm removes the data permanently, and there is practically no way of recovering deleted files reliably. trash-cli fills this role and lets you 'trash' files and directories and lets you recover 'trashed' items.
There are multiple ways to install trash-cli. It is open source and instructions can be found on Github.
As a side note: In this article, I will work with aliases. You can pick whatever alias you want, but it is not recommended to overwrite rm for trash-cli. Overwriting rm can cause issues with scripts, applications, and other features. That said, make sure not to overwrite an already-used command.
Add the aliases by adding them to your ~/.bashrc file and load it with source ~/.bashrc. It may vary depending on your setup.
You can move files into the trash can with trash or trash-put. It works with files and directories. I've been using it with the alias tm as it is close to rm.
alias tm="trash"You can use trash-list to show the content of the trash can.
$ trash-list
2024-02-03 22:53:27 /home/user/data/file2
2024-02-03 22:53:27 /home/user/data/file4
alias tmls="trash-list"$ trash-list | grep -i file4
2024-02-03 22:53:27 /home/user/data/file4
Side note: -i in grep makes the search case-insensitive.
alias tmgr="trash-list | grep -i"The following directories store the trashed items:
~/.local/share/Trash/files and /root/.local/share/Trash/files # trashed with sudo
du -sh ~/.local/share/Trash/filesalias tmdu="du -sh ~/.local/share/Trash/filesThe advantage of trash-cli is the possibility to recover 'trashed' items.:
$ trash-restore
0 2024-02-03 23:05:54 /home/user/data/file5
1 2024-02-03 23:05:54 /home/user/data/dir3
2 2024-02-03 23:05:54 /home/user/data/file7
3 2024-02-03 23:05:54 /home/user/data/dir4
4 2024-02-03 22:53:27 /home/user/data/file4
What file to restore [0..4]:
Choose a single file or directory or multiple items with e.g. 2-3. The chosen items will be restored to their original destination.
alias tmre="trash-restoreYou can't restore an item when an item with the same name is in the original path.
Refusing to overwrite existing file "file3".
There is an --overwrite option, but it is not working for me and I haven't really looked into it as I don't need it that often.
There are multiple ways to do so. I haven't added any aliases for those options, but feel free to do so.
trash-emptyn days:trash-empty ntrash-empty 30trash-rm NameOfItem # removes all items called NameOfItemtrash-rm '*.iso' # removes all .iso filestrash-rm /path/of/items # should remove all items with a specific path, but it is not working for meEmptying the trash can be automated with cron jobs.
I run it once a day to delete all items that have been trashed more than 7 days ago, but please modify as you wish:
crontab -e > add 20 4 * * * trash-rm 7 - runs every day at 4:20 am
It saved me multiple times, and I can recommend it. I've gotten used to using tm instead of rm, which can be annoying on systems I don't manage, but this is a small price to pay. The source code can be found on Github.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。