File Deletions Control in Ubuntu using trash-cli
trash-cli is a command-line tool designed for managing
file deletions in a way similar to the trash bin functionality found in
graphical user interfaces. It offers a set of commands that allow users
to move files to the trash, restore files from the trash, view the
contents of the trash, and empty the trash. Using trash-cli
can reduce the risk of losing important files due to accidental misuse
of the rm command. Moreover, it provides a safety net for
file deletion in the command-line environment, making file recovery
possible.
Installing trash-cli
On Ubuntu systems, you can install trash-cli using the
APT package manager:
1 | sudo apt update |
This command installs trash-cli and its necessary
dependencies.
Main Commands of
trash-cli
trash-putortrash- Moves files or directories to the trash.
- Usage:
trash-put [file or directory] - Example:
trash-put example.txtmoves the fileexample.txtto the trash.
trash-list- Lists the files and directories currently in the trash.
- Example:
trash-listshows all items in the trash with their deletion dates.
trash-restore- Restores files from the trash.
- This command lists all files in the trash and allows you to select a file to restore.
- Example: Run
trash-restoreand follow the prompts to choose and restore a file.
trash-rm- Removes specific files from the trash based on a pattern.
- Usage:
trash-rm [pattern] - Example:
trash-rm *example*removes all files containing “example” from the trash.
trash-empty- Empties all files from the trash or files that have been in the trash for a specified number of days.
- Usage:
trash-empty [days] - Example:
trash-empty 30will remove all files that have been in the trash for more than 30 days.
Clear the Trash every Monday Morning:
Open a terminal.
Enter
crontab -eto edit yourcronjob list.In the editor, set or update the line to execute at 2 AM every Monday by adding or modifying the following line:
1
0 2 * * 1 /usr/bin/trash-empty
This cron expression means:
0 2: At 2:00 AM.* * 1: Every day of every month, if it is Monday (1represents Monday), execute the command.
With this setting, the system will automatically execute the
trash-empty command at 2 AM every Monday, emptying all the
files in the trash.
After saving and exiting the cron editor, the
cron service will automatically load the new job. You can
confirm that the task is correctly scheduled by checking the
cron logs:
1 | grep CRON /var/log/syslog |
This command helps you see recent log entries related to
cron, ensuring that your task has been recognized and
scheduled correctly.
This approach allows you to flexibly adjust the timing of scheduled tasks to meet different maintenance needs and personal preferences.