Categories: Ubuntu

How to Automatically Empty the Trash in Ubuntu 20.04

When you delete a file in Linux, it is not immediately deleted from the system. Instead, it is moved to the Trash in case you need to restore it. Trash is the first place where you look for the accidentally deleted files. Because the file is not removed from the system, it continues to take space on the system and in the end, can fill up large storage space. Although we can empty the trash manually to avoid wasting system space, however, we often forgot to do such a simple task. Luckily, Linux OS offers a utility known as “Autotrash” that can automatically delete the data in the Trash based on the options you specify.
Sponsored

In this post, we will explain how to install the Autotrash utility to automatically empty the trash in Ubuntu. This way you can get rid of old and useless files and make room for important files.

Note: The commands discussed here have been tested on Ubuntu 20.04 LTS (Focal Fossa). You must have be root user or have sudo privileges to install Autotrash on your Linux system.

Installing Autotrash

Autotrash utility is by default not installed in the Ubuntu OS. However, it is available as a snap package that can be easily installed using the snapd.

Step 1: Install Snapd

First, install snapd on your system. Execute the below command to do so:

$ sudo apt install snapd

Enter sudo password, after that the installation of snapd will be started on your system. Once snapd is installed, you can use it to install Autotrash snap.

Step 2: Install Autotrash

Execute the below command in Terminal to install Autotrash utility on your system:

$ sudo snap install autotrash-unofficial

Once the Autotrash is installed you will see the below output:

Step 3: Create an alias for Autotrash

After installation of Autotrash via snap, you will have to run every command by typing the complete snap name that is autotrash-unofficial. You can just use autotrash instead of complete snap name autotrash-unofficial by creating an alias as follows:

$ sudo snap alias autotrash-unofficial autotrash

The alias will support calling the autotrash-unofficial as just autotrash.

Note: In case, you want to remove the alias, execute the below command:

$ sudo snap unalias autotrash

To verify if the Autotrash has been successfully installed and to view its version, execute the below command in Terminal:

$ autotrash -V

Using Autotrash to Automatically Empty the Trash in Ubuntu

Autotrash allows you to automatically empty the trash based on certain options that you need to specify. It can automatically delete files based on age, size, and free space on the trash location.

Sponsored

Automatically Deleting Files older than x number of days

This is the most commonly used Autotrash functionality. The following command syntax can be used to automatically delete the files that exist in the trash for more than x number of days:

$ autotrash -d [number of days]

This will automatically delete the files that have been in the trash over the period you specified. For instance, the below command will delete the files that have been in the trash for more than 45 days:

$ autotrash -d 45

If you want to restore any deleted file, you can do that within 45 days.

Automatically Deleting files based on free-space left

Using the Autotrash utility, you can make sure a specific amount of disk space is free in the Trash location. Here is the syntax of the command:

$ autotrash –min-free [free-space in MB]

For instance, the below command will make sure there is a minimum of 512 MB of free space:

$ autotrash –min-free 512

Automatically Deleting M number of megabytes

Using Autotrash –delete option, you can allow it to automatically delete at least M megabytes, removing the oldest trashed items first. Here is the syntax of the command:

$ autotrash –delete [data size in megabytes]

This option removes the trashed entries, not the individual files. To explain it, consider you have an older trashed directory of 1 GB. If you request Autotrash to delete 512 MB (M=512), it will remove 1 GB.

$ autotrash –delete 512

Uninstalling Autotrash

In case you no longer need Autotrash on your system, you can easily remove it using the following command in Terminal:

$ sudo snap remove autotrash-unofficial

Enter password for sudo, after which Autotrash will be removed from your system.

In this post, we have explained how to install Autotrash on Ubuntu system. Now the Autotrash will automatically delete the files residing in the Trash based on the conditions you specified.

Ubuntu Server Admin

Recent Posts

How to Install nvidia-smi on Ubuntu or Debian Linux

In this article, we will see how to install nvidia-smi on Ubuntu or Debian Linux.…

8 hours ago

How to Install clang tool on Ubuntu or Debian Linux

In this article, we will see how to install clang tool on Ubuntu or Debian…

1 day ago

How to resolve Ubuntu 20.04 Container Signature Errors on Raspberry Pi ARM Devices

When working with Docker containers on Raspberry Pi devices, you might encounter frustrating signature verification…

2 days ago

How to fix DNS Resolution Issues with OpenVPN on Ubuntu 18.04

You’ve recently upgraded to Ubuntu 18.04 and found that your OpenVPN connection no longer resolves…

2 days ago

How to Fix Ubuntu 18.04 System Monitor Launch Issues

Have you ever tried to open System Monitor on your Ubuntu 18.04 system only to…

3 days ago

What is System Hardening? Essential Checklists from OS to Applications

System hardening means locking down a system and reducing its attack surface: removing unnecessary software…

3 days ago