1. Introduction
Deleting files and directories in the Linux operating system (OS) generally moves them to the trash directory, which is normally located at ~/.local/share/Trash/. So, emptying the trash usually frees up storage space and improves system performance. Moreover, it can be essential for privacy as the deleted data might contain sensitive information.
In this tutorial, we’ll discuss various commands to empty the trash directory, such as rm, find, and trash-cli**. Lastly,** we’ll discuss creating a cron job to automatically empty the trash.
2. Using the rm Command
The rm command is an obvious choice when we want to remove files and directories:
$ rm -r ~/.local/share/Trash/*
Here, the -r option performs a recursive operation, which tells the rm command to delete all data from the trash. Next, the complete trash directory path has been provided. The asterisk (*) at the end acts as a wildcard within the Bash globbing mechanism to match all files and directories inside the trash.
This should empty the entire trash, which can be verified using the list (ls) command:
$ ls ~/.local/share/Trash
If the command doesn’t display anything, the trash is empty.
We can also include the -i option, which asks for our permission before deleting any files or directories from the trash:
$ rm -ri ~/.local/share/Trash/*
While this method provides an extra layer of security, it’s also more time-consuming as we need to confirm each deletion manually.
3. Using the find Command
We can primarily use the find command to locate files or directories. However, find can also empty the trash:
$ find ~/.local/share/Trash/* -delete
Here, the find command first accesses the provided path, and later, the -delete option deletes everything from that path.
4. Using trash-cli Command
The trash-cli is a handy tool for managing trash but requires prior installation.
For example, we can install it on Ubuntu using apt:
$ sudo apt install trash-cli
After its installation, we can empty the entire trash with the trash-empty command:
$ trash-empty
Moreover, the trash-list command displays the contents of the trash:
$ trash-list
2023-11-05 07:43:37 /home/baeldung/Downloads/file3
2023-11-05 07:43:37 /home/baeldung/Downloads/file2
2023-11-05 07:43:37 /home/baeldung/Downloads/file1
Now, we can decide whether we want to empty the entire trash or not.
5. Using a cron Job
The earlier methods require manual execution of the commands. However, we can perform this process automatically using a cron job.
To do so, we edit our crontab file:
$ crontab -e
Next, we can make the following changes, which initiate the cron job to empty the trash every midnight:
0 0 * * * rm -r ~/.local/share/Trash/*
Each of these asterisks represents a different aspect of time, starting from left to right:
- minute (0 – 59)
- hour (0 – 23)
- day of month (1 – 31)
- month (1 – 12)
- day of week (0 – 6) (Sunday to Saturday)
So, here we replaced the first two asterisk characters with 0.
Similarly, we can empty the trash every Saturday at midnight:
0 0 * * 6 rm -r ~/.local/share/Trash/*
In the same way, we can decide on other combinations as well.
Moreover, we can also create a Bash script for this purpose:
0 0 * * 6 ~/scripts/empty_trash.sh
The cron job is configured to run the Bash script named empty_trash.sh every Saturday at midnight. Within this script, there’s a command that empties the trash. By providing the script with its full path, we let the cron job know precisely where to find and execute it at the scheduled time.
6. Conclusion
In this article, we discussed the rm, find and trash-empty commands to empty the trash in the Linux OS. The rm command performs simple deletion tasks more efficiently than the find command. On the other hand, the find command is a better choice where the search criteria are required for deletion. Compared with the rm and find commands, trash-cli provides better options to manage and empty the trash without specifying the directory path.
These commands require manual execution, so we created a cron job that empties the trash automatically at the desired time. However, caution is needed when we empty the trash as it can be challenging to recover the data later. So, it’s crucial to create a backup of any critical data or to retrieve it before the scheduled cron job is executed.