trash-cli

trash-cli usage guide

trash-cli

Installation

Install from apt

sudo apt install trash-cli

Or install from pip

sudo pip install trash-cli

Usage

Delete files and folders

trash-put file.txt directory

This example deletes the file file.txt as well as the folder directory

List trash

trash-list

Restore trash

trash-restore

You will be prompted to choose what to restore

Remove trash

trash-rm file.txt

This removes file.txt

Empty trash

trash-empty

Removes everything from trash

trash-empty 10

removes any trash older than 10 days

Or, to make this automatic, set up crontab:

0 3 * * * $(which trash-empty) 30

Which empties any trash older than 30 days at every 3AM

For desktop users, it may be a good idea to have the job run every hour instead:

0 * * * * $(which trash-empty) 30

Which empties any trash older than 30 days at minute 0 of every hour

Daily trash empty timer using systemd

Create the following script at /usr/local/bin/empty-trash.sh:

#!/bin/bash
# Empty all trash older than 30 days
yes | trash-empty 30

Make the script executable for everyone:

sudo chmod 0755 /usr/local/bin/empty-trash.sh

Create the following service at /etc/systemd/user/empty-trash.service to wrap the script into a service for all users. Alternatively, create the script at ~/.config/systemd/user/empty-trash.service to make the service only available to the current user.

[Unit]
Description=Empty Trash for Current User

[Service]
Type=oneshot
ExecStart=/usr/local/bin/empty-trash.sh

Create the following timer at /etc/systemd/user/empty-trash.timer (or ~/.config/systemd/user/empty-trash.timer for the current user only):

[Unit]
Description=Daily Trash Empty Timer

[Timer]
# Run daily at 4 am
OnCalendar=*-*-* 04:00:00
# Catch up missed runs
Persistent=true

[Install]
WantedBy=timers.target

Enable and start the timer:

systemctl --user daemon-reload
systemctl --user enable --now empty-trash.timer

To check its status:

systemctl --user status empty-trash.timer

To view the logs:

journalctl --user -u empty-trash.service

References

https://linux.cn/article-10029-1.html

https://www.2daygeek.com/trash-cli-command-line-trashcan-linux-system/

https://pypi.org/project/trash-cli/

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

ChatGPT

https://wiki.archlinux.org/title/Systemd/Timers

Content Licensed under CC BY-SA 4.0. Code licensed under the MIT License.
Last updated on Feb 04, 2025 04:43 UTC