惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
博客园_首页
U
Unit 42
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
IT之家
IT之家
G
Google Developers Blog
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
Jina AI
Jina AI
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
小众软件
小众软件
H
Help Net Security

Ittavern.com

Wimage - Hosting Open-Source Image Uploader with Podman and external S3 Storage Switching from Hugo to picopaper Encryption using SSH Keys with age in Linux ETag in nginx - Simple Resource Caching Sending nginx Logs to Loki with Grafana Alloy How to: Cisco ISE backup to SFTP repository with public key authentication Dummy IP & MAC Addresses for Documentation & Sanitization Deploying ISSO Commenting System for Static Content using Docker Generate a Vanity v3 Hidden Service Onion Address with mkp224o ssh-audit Primer - Audit your SSH Server mtr - More Detailed Traceroute - Network Troubleshooting My Personal Backup Strategy - August 2024 iperf3 - User Authentication with Password and RSA Public Keypair Bandwidth Measurement using netcat on Linux Getting started with rsync - Comprehensive Guide Cron Jobs on Linux - Comprehensive Guide with Examples SSH Server Hardening Guide v2 Port Knocking with knockd and Linux - Server Hardening Getting started with rclone - Data transmission Getting started with dig - DNS troubleshooting Getting started with Fail2Ban on Linux Getting started with netcat on Linux with examples URL explained - The Fundamentals Troubleshooting Asking The Right Questions Create tmux layouts using bash scripts Getting started with tcpdump - Ittavern.com Curl on Linux - Reference Guide Getting started with nmap scripts My Offsite Backup - March 2023 Getting started with iperf3 - Network Troubleshooting
Adding a trash can to Linux with trash-cli
2024-02-04 · via Ittavern.com

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.

Installation #

There are multiple ways to install trash-cli. It is open source and instructions can be found on Github.

Working with Aliases #

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.

Moving files into the trash can

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:
alias tm="trash"

Showing files and dirs in the trash can

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:
alias tmls="trash-list"

Looking for specific files in the trash can #

$ 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:
alias tmgr="trash-list | grep -i"

Disk Space #

The following directories store the trashed items: ~/.local/share/Trash/files and /root/.local/share/Trash/files # trashed with sudo

You can check the used space of the trash can with the following command:
du -sh ~/.local/share/Trash/files
Alias:
alias tmdu="du -sh ~/.local/share/Trash/files

Getting things out of the trash

The 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:
alias tmre="trash-restore

You 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.

Emptying the trash can

There are multiple ways to do so. I haven't added any aliases for those options, but feel free to do so.

Removes all items from trash can:
trash-empty
There is no confirmation prompt!
Removes all items that have been deleted more than n days:
trash-empty n
trash-empty 30

Removing specific items #

Removes specific items from the trash can:
trash-rm NameOfItem # removes all items called NameOfItem
trash-rm '*.iso' # removes all .iso files
trash-rm /path/of/items # should remove all items with a specific path, but it is not working for me

Cron #

Emptying 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

Conclusion

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.