I will show you how to do auto clean of your downloads directory on Linux. In other words, with some help of scripts and CRON, you will achieve two-steps auto cleaning. At first, all files will be moved to “downloads trash”. Then, after certain amount of time (e. g. a month), they will be erased.

Please note that due to bash scripting, this tutorial applies only to Linux systems.

TLDR: If you want to just see the solution, without reading the whole article, scroll down to the Summary section.

 

Why should I care?

If you work daily on your system, you surely download some files from time to time. After few months, it’s likely this directory turns into a mess. Especially, if you keep your files there and don’t move them to better suited folders. Examples include:

  • your documents like Word and Excel files, that you haven’t moved to Documents dir,
  • some photos or graphic files you basically should have moved to Images.

Even though you might be okay with it for a while, with a time you will probably find a need to delete some files. One cause is when summed amount of taken space within Downloads exceed few gigabytes. You usually need to delete some old stuff then. You cannot just simply select all and delete, because you will remove important files as well. What I suggest to do is to discipline yourself beforehand. Immediately move files to appropriate locations, when only you get them on your PC. To help you maintain this order, as well as avoid overloading Downloads, let’s clean this directory automatically. To avoid accidental removal, we will implement some transitional space, where files will wait until final deletion.

 

Plan of downloads directory auto clean

This is the idea:

  1. When system starts, move all files from Downloads to Downloads/Trash subdirectory.
    It will make your former dir free from mess. Additionally, if you forget to move some important files to their proper place, you will still have a chance to do so. This is the mentioned transitional space.
  2. Filter out files older than a month (or any period of time you like) and delete them completely.
    For the stuff you don’t really needed for all this time, don’t bother yourself with manual deletion. Let the script do this for you.

 

Move files from dir to the bin

Let’s start from beginning. We want to perform actions in Downloads directory. Create a bin folder inside, for example Trash. What we want to do is to move everything except Trash directory to Trash directory. It’s simple, doesn’t it? Here are the commands:

$ cd ~/Downloads
$ mv ./!(Trash) ./Trash

Unfortunately, bash doesn’t allow such convenient regex by default, but we can enable it with shopt command. This is a tool that allows to use some extra features of bash. We need specifically:

  1. extglob – to enable advanced pattern matching;
  2. dotglob – to turn on matching files starting with a dot. Sometimes they are left when e.g. system created a lockfiles and for some reason didn’t delete it.

Each option we turn on with -s flag. You can read more about shopt on The Shopt Builtin.

To get that all together, let’s create a script, say, clean_downloads.sh:

#!/bin/bash

cd ~/Downloads
shopt -s extglob
shopt -s dotglob
mv ./!(Trash) ./Trash
shopt -u dotglob
shopt -u extglob

Remember to turn on script execution with $ chmod +x clean_downloads.sh before you run it.

 

Remove files from the transient space and finish downloads directory cleaning

Next step is to get into the Trash subdir and filter out the old files. For this, we need to use find command. Some of it’s arguments will be useful:

  • . : search in current directory
  • -atime +30: last time of access was more than 30 days,
  • -delete: on every matching file, execute deletion.

To try this without risk of deleting files, use this command first without last parameter:

$ cd ~/Downloads/Trash
$ find . -atime +30

Now we can include this lines in our script. Additionally, let’s parameterize number of days:

#!/bin/bash

MAX_DAYS=30

# Move files
cd ~/Downloads
shopt -s extglob
shopt -s dotglob
mv ./!(Trash) ./Trash
shopt -u dotglob
shopt -u extglob

# Remove files from Trash older than MAX_DAYS days
cd ~/Downloads/Trash
find . -atime +${MAX_DAYS} -delete

Now, you can run it to make the cleaning. But let’s go one step further and execute it automatically whenerver the system starts. With this, we would totally get this task out of our heads.

 

Add script to the crontab so as to achieve auto execution

Cron is a powerful tool that let’s you precisely execute certain commands on given time. We will now use pretty basic scheduling entry, called @reboot. It basically mean “do this every time the system starts”. Let’s assume we keep our clean_downloads.sh in our ~/scripts directory. So, we need to add following entry with crontab -e:

@reboot ~/scripts/clean_downloads.sh

Since every user has it’s own crontab file, you can have your script safely auto started. In other words, it won’t affect another accounts.

TLDR / Summary

Following structure from my previous article (How to automatically clean your modified code?), here is the complete solution too. Adjust as you like:

#!/bin/bash

MAX_DAYS=32

# Move files
cd ~/Downloads
shopt -s extglob
shopt -s dotglob
mv ./!(Trash) ./Trash
shopt -u dotglob
shopt -u extglob

# Remove files from Trash older than ${MAX_DAYS} days
cd ~/Downloads/Trash
find . -atime +${MAX_DAYS} -delete

Put the script job into crontab with @reboot flag.

Sources

Leave a Reply

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.