When we delete a file or folder from our system, it is moved to the Recycle Bin (Linux) or the Trash (Windows). Again and again, we have to get rid of these mostly useless files and folders that are in our system crash to make room for other important data. If you want to set your Debian to automatically take care of the system trash over a period of time, you can use the command-line utility called Autotrash.

Autotrash is a utility that searches a trash folder on FreeDesktop.org for information about its contents and then deletes part of the trash depending on the options you specify.

This article explains how to install the Autotrash utility, find help, and use it. We’ll explain the procedure with some examples that will help you understand the widely used features of this utility.

We have run the commands and procedures mentioned in this article on a Debian 10 Buster system.

Install Autotrash through the Debian Terminal

We will install the Autotrash CLI through the Terminal application. You can launch the Terminal through the Application Launcher search as follows:

Debian Terminal

Please note that only an authorized user( root or sudo) can add/update, remove and configure software on a Debian system.

Step1: Update your system’s repository Index

In order to install the latest version of the software available in Debian’s online repositories, your system’s repository index needs to be up-to-date.

Run the following command as sudo in order to update your system’s local repository index with that of the Internet.

$ sudo apt-get update

Update packages

The system might ask you for the password for sudo. Enter the password and your repository index will be updated.

Step 2: Install the Autotrash CLI

Now, you can install the autotrash utility by running the following command as sudo in your Terminal application:

$ sudo apt-get install autotrash

Install the Autotrash CLI

The system might prompt you with a Y/n option before starting the installation procedure. Please enter Y to continue.

Step 3: Verify Autotrash installation (optional)

You can check the version number of your installed autotrash software and also verify if it is indeed installed on your system. Run the following command to do so:

$ autotrash --version

Or,

$ autotrash -V

Check autotrash version

The results ensure that autotrash version 0.1.5 is installed on my Debian.

Using Autotrash CLI (with examples)

The autotrash help will tell you all you can do with this application. You can view the help as follows:

$ autotrash --help

Using Autotrash CLI

For even more details, you can view the manpage of autotrash by using the following command:

$ man autotrash

Now we will present some examples that will elaborate on how you can make the best use of this utility:

Example 1: Delete Files older than x number of days

This is the most widely used feature of Autotrash. You can use the following command syntax to automatically remove the files residing in your system trash for x number of days:

$ autotrash -d [days]

For example, the following command will remove the files that have completed their 30 days in the trash can.

$ autotrash -d 30

Delete files that are older than 30 days

This gives you enough time to restore any files that you might wish to- during the 30 days.

The following usage of the command lets you apply this feature for not only your trashed files but also deleted files of all the user’s using the system:

$ autotrash -td [days]

Example:

$ autotrash -td 30

Example 2: Delete files based on free-space left

Max-free

You can only use autotrash to purge files if there is less than M megabytes of free space left at the trash location. As an example, if you set this to 1024, then autotrash will only start to work if there is less than 1GB of free space in the trash. Till that time, autotrash will just exit normally without scanning the Trash directory.

Example:

$ autotrash --max-free 1024 -d 30

Min-free

Similarly, you can use autotrash to make sure there is a minimum of M megabytes of free space.

Example:

$ autotrash --min-free 1024

You can even combine the two commands to make sure that autotrash starts reading the trash if there is less than 2 GB of free space, and then keep a check. The command will then remove files older than 30 days and also remove extra files to ensure that 1 GB of space is always available.

$ autotrash --max-free 2048 --min-free 1024 -d 30

Example 3: Delete M number of megabytes

You can use the utility to purge at least M number of megabytes, deleting the oldest trash addition first. This feature uses trash entries rather than the individual files This means that if your oldest trashed item is a 1GB directory, and you request at least 10MB to be removed (M=10), autotrash will remove 1GB.

$ autotrash --delete M

Example

$ autotrash --delete 1024

The examples we presented will help you in configuring even more customizable ways to empty trash. Now you do not need to worry about space wastage on your system because of the system trash The autotrash utility will automatically take care of clearing your unwanted files based on your defined criteria.

Automatically Empty Trash on Debian through Autotrash CLI
Avatar

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.