If you are low on disk space in Ubuntu, you may want to automate the emptying of your trash. A file sent to the trash still takes up space on your hard drive. This is so it can be easily recovered if you find you need the file after all. However, if you never empty your trash, it keeps growing and takes up valuable space on your hard drive.
To avoid having to think about the menial task of emptying your trash now and then, you can install Autotrash, which is a Python script that purges files from your trash based on their age or on the amount of disk space left on your computer.
This post shows you how to install Autotrash and lists some sample commands using Autotrash and what those commands do. Then, we show you two methods for automating emptying the trash.
The first method uses crontab to schedule a task to run Autotrash with specific parameters. The second method adds Autotrash to the list of programs that run at startup time. Because both of these methods perform the same task, select one to use.
To install Autotrash, select Accessories | Terminal from the Applications menu.
To add the repository for Autotrash, enter the following line at the prompt in the Terminal window and press Enter.
sudo add-apt-repository ppa:bneijt/ppa
Enter your password when prompted.
To retrieve the updated package list from the newly added repository, enter the following line at the prompt in the Terminal window and press Enter.
sudo apt-get update
To install Autotrash, enter the following line at the prompt in the Terminal window and press Enter.
sudo apt-get install autotrash
If you want to close the Terminal window, enter “exit” (without the quotes) at the prompt and press Enter. If you want to use the crontab method for automating emptying the trash, you can leave the Terminal window open for now.
Sample Uses of Autotrash
These samples illustrate manually setting up Autotrash to empty the trash. If you manually run any these commands, you will have to do so every time you boot into Ubuntu. A better way to use these commands is one of the two methods for automating emptying the trash that follows these examples.
To purge any file that has been in the trash for more than 30 days, enter the following line in a Terminal window.
autotrash –d 30
To only purge files from the trash that are older than 30 days if there is less than 1GB of space left in the trash file system, enter the following line in a Terminal window.
autotrash –max-free 1024 -d 30
To purge files from the trash, oldest files first with no restriction on how old the files are, until there is at least 2GB of free space in the trash file system, enter the following line in a Terminal window.
autotrash –min-free 2048
NOTE: By default, the oldest files in the trash are deleted first, if no other deletion order is specified.
For more information on how to use Autotrash and for more example of its use, enter “man autotrash” (without the quotes) in a Terminal window.
Use Crontab to Automate Emptying the Trash
The first method of automating emptying the trash, involves the crontab utility. This utility allows you to automate many tasks by setting a schedule to run programs or utilities.
If you closed the Terminal window, reopen it by selecting Accessories | Terminal from the Applications menu, as mentioned at the beginning of this post.
We find it easy to use gedit to edit the crontab file to schedule tasks. To do this, enter the following line at the prompt in the Terminal window and press Enter.
This creates an environment variable that tells Ubuntu to use gedit as the default text editor.
To open the crontab file for editing, enter the following line at the prompt in the Terminal window and press Enter.
If you have never scheduled a task using crontab, a message displays, telling you that there is no crontab for you and an empty one is being used.
The crontab file opens in a gedit window. Add a line to the end of the crontab file to schedule Autotrash to run with the desired parameters.
For example, to have Autotrash run daily to delete files in the trash that are older than 30 days, enter the following line into the crontab file.
@daily /usr/bin/autotrash -d 30
The “@daily” text tells the crontab file to run the specified command once every day. For more information about using crontab, see our post, Automating Tasks in Linux Using Crontab.
Save the crontab file by clicking the Save button on the toolbar.
To close gedit, select Quit from the File menu.
Close the Terminal window by entering “exit” (without the quotes) at the prompt and pressing Enter.
Run Autotrash When You Boot Your Computer
To automatically run Autotrash when you boot into you Ubuntu system, you can add the desired Autotrash command to the list of Startup Applications. To do this, select Preferences | Startup Applications from the System menu.
The Startup Applications Preferences dialog box displays. Click Add.
Enter a name for the startup program in the Name edit box, such as “Autotrash” (without the quotes). Then, enter the desired Autotrash command to run in the Command edit box. As an example, if you want to purge any file that has been in the trash for more than 30 days every time you boot your computer, enter “autotrash -d 30” (again, without the quotes) in the Command edit box. Enter a comment for the startup program in the Comment edit box. Click Add.
Your new startup program for Autotrash is listed on the Startup Programs tab in alphabetical order. Make sure there is a check mark in the check box to the left of the new startup program. Click Close.
Now, your trash will be emptied of files that meet the criteria you specified for Autotrash every time you boot into Ubuntu.
by Lori Kaufman