Ubuntu – How to set up Ubuntu 16.04 LTS to automatically empty the trash based on parameters

16.04deletetrash

There is a way to periodically blindly empty the trash on a Ubuntu 16.04 LTS system for a specific user.

Start with a script that contains the following lines:

#!/bin/bash
rm -rf ~/.local/share/Trash/*

then use cron to have it run on a periodic basis.

(In fact, this works under Raspbian and other versions of Debian, also)


But what if it would be preferable to keep files for xx days, or until free space drops below yy, or some other parameter?

The question is in the title: How can I set up Ubuntu 16.04 to automatically empty the trash based on parameters?

Best Answer

Turns out there is a program called autotrash that works under Ubuntu 16.04 LTS.

Here is an article that describes how it works:

Automatically Empty the Trash in Ubuntu

--> This article is informative but is older, so do not follow the install instructions.

Instead, just do this:

sudo apt-get update
sudo apt-get install autotrash

Then the program is executed by entering the autotrash command with arguments.

This can be run periodically by cron, and based on specific parameters:

Those arguments are described in detail in the

man page for autotrash for Ubuntu 16.04 LTS

EXCERPT:

NAME autotrash — program to automatically purge old files from the trash

SYNOPSIS autotrash [-d purge_n_days_old]

   autotrash [--delete number_of_megabytes_to_purge]

   autotrash [--keep-free number_of_megabytes_to_free]

DESCRIPTION autotrash is a program that looks in a FreeDesktop.org Trash folder for information on it's contents and then purges a part of the trash depending on the options.

   The most common option is -d, which will purge files that have been  in
   the trash for more then a given number of days.

   The  option --delete will remove at least the given number of megabytes
   from the trash, removing the oldest trash first.

   Using --keep-free will make sure at least the given number of megabytes
   of  free  space  is available, by automatically setting --delete to the
   right value. For example, to  keep  at  least  a  1GB  of  free  space,
   removing files from the trash if needed, use autotrash --keep-free 1024

The man page goes on to describe many other options.