How to Delete Old Files In A Folder Automatically In Linux

If you have a habit of storing all your temporary files on your desktop (or any other folder) and forgot to remove them later, you will find that your computer get messed up easily with tons of files that you have no use for. Once that happens, cleaning up your computer becomes a tedious task and a troublesome chores. Here’s a quick and easy way to watch a folder for old files and delete them automatically.

The command to achieve this is:

find /path/to/directory/* -mtime +t -exec rm {} \;

where +t is the time criteria to search the files.

For example, I took a lot of screenshots on my Gnome shell desktop and it resulted in tons of redundant images in my Pictures folder. I used the following command to delete old images in the folder:

find /home/damien/Pictures/* -mtime +1 -exec rm -f {} \;

This will find all files in the Pictures folder that are more than 1 day old and (force) remove them all.

More options

1. You can change the path to point to any folder. Make sure you use absolute path.

2. You can change the time interval. -mtime refers to number of days while -mmin refers to number of minutes.

3. You can set the time criteria. A “+5” value means more than 5 days/minutes while a “-5” value means 5 days/minutes or less. You can also use a combination like “+5 -10” to denote an interval that is more than 5 days/minutes and less than 10 days/minutes.

4. The “exec” command will work for command like “cp”, “mv”, “rm”, “rmdir” etc. Other than deleting old files, you can get it to perform other tasks as well, such as moving a file out of Dropbox folder.

Automating the process

There are several ways that you can automate the process.

1. Run on startup

Copy the following code and paste them onto a text editor. Remember to change the file path, time interval, time criteria to suit your needs. Save the file as “find-and-delete” in your Home folder.

find /path/to/directory/* -mtime +t -exec rm -f {} \;

Open the File Manager app and browse to the Home folder. Right click on the “find-and-delete” file and select Properties. Go to the Permissions tab and check the box “Allow executing file as program”.


Next, open up the Startup Application and add the the “find-and-delete” file to the list.

2. Cron

If you need the script to run on a regular interval, you can set a cron job.

Open a terminal and type the following:

crontab -e

If it prompts you to select an editor, enter “2” (for nano).

The structure for cron setup is

minute hour day-of-month month day-of-week command

To run the script at every hour, enter

00 * * * * /path/to/find-and-delete

at the end of the crontab file, on a new line.


Save (Ctrl + o) and exit (Ctrl + x) the crontab.

Gnome Schedule

If using crontab from the terminal scares you out, you can use Gnome-schedule to schedule the task.


That’s it. The methods above are tested on Ubuntu, but it should work on all Linux distro.

Image credit: Delete by Big Stock Photo.


Damien Oh started writing tech articles since 2007 and has over 10 years of experience in the tech industry. He is proficient in Windows, Linux, Mac, Android and iOS, and worked as a part time WordPress Developer. He is currently the owner and Editor-in-Chief of Make Tech Easier.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox