How to delete files older than 2 hours automatically every minute?

How to delete files older than 2 hours automatically every minute?

Simple, right? Well, not in my case. ( I am using Linux Mint 17.1 Rebecca )

Short version, only with code:
Install tmpreaper:
sudo apt-get install tmpreaper

Create bash script (
tmpreaper --all --protect .localized 2h /home/dvs/mytest/

Make it executable:
chmod +x

On terminal
crontab -e

Add to the crontab(add the variable PATH where is located tmpreaper):


Long version with explanations:
First I search how to delete files older than some time. I read about two approaches from here – stackexchange and here superuser:
1 – find /a/b/c/1 /a/b/c/2 -type f -mtime +3 #-delete
2 – tmpwatch (or tmpreaper in Ubuntu)

Tempted by the comment from WojonsTech -> “It’s simple and works well in many cases”, I decided to try to do it with tmpreaper. Well, it turns out to be that it was not more simple, and next time, I would prefer to first give a try to the built-in commands than installing other software 😉

But I will put here my experience and how I solved the problem using tmpreaper.

About tmpreaper I can advice you two links: thegeekstuff and man pages

In my case I wanted to be able to delete files that are older that 2 hours and I wanted to check this every minute.
I installed tmpreader:
sudo apt-get install tmpreader

Than, I made a quick test with a test file that I created under the folder mytest – this code delete files with more than 5 seconds:
tmpreaper 5s /home/dvs/mytest/

It works! So far, so good!

I read in the thegeekstuff post, “By default when you install tmpreaper, it will put an entry in crontab ( /etc/cron.daily/tmpreaper ). It will read the options from /etc/tmpreaper.conf and execute tmpreaper command based on those options.” But, cron.daily it was not what I wanted…because I was looking for runnig the cron every minute. So, I thought that I will write a cronjob to do what I wanted.

“crontab stands for “cron table,” because it uses the job scheduler cron to execute tasks” – I recommend to read this brief post – computerhope

First things first, I begin by writing a test script with the command to run tmpreader (


tmpreaper --all --protect .localized 5s /home/dvs/mytest/.

It will remove files with more than 5 seconds.

Then I made the file executable:
chmod +x

I run the file:

It worked! So far, so good!

Then, I add the script to the crontab by:
crontab -e

Using the computerhope post, I wrote in the crontab the following line to run every minute:
* * * * * /home/dvs/

Just in case:
service cron restart

And that should be it, but it was not working! I waited 1 minute…2 minutes…the file was still there, there is some problem!!!

First, I verify if the cronjob was working by seeing the log from the cron.

I followed the steps described by user93341 to enable the logs in askubuntu:
Open the file

Find the line that starts with:

uncomment that line, save the file, and restart rsyslog:

sudo service rsyslog restart
You should now see a cron log file here:

tail -f /var/log/cron.log

It showed that it was working every minute as expected.

The big help came from the comment Jonathan Leffler in stackoverflow with At least 95%, and probably nearer 99%, of the time when a ‘command works when run from command line and not from cron’, then the ‘environment’ is to blame.“.

Than, as recommend in stackoverflow I added to the crontab (by: crontab -e) the following line to identify which were the environment variables seen by the cron:
* * * * * env > /tmp/cron.env.$$

The contents of the file were:
(…some more variables…)

But I am not the only one with this problem- “Linux cron job fails to execute part of script which works in shell” – stackoverflow, as stated by Aslan – “That is probably because hdparm is not in the PATH of the cron when the script is executed through cron.”

So the next step was making a quick search where was the tmpreaper and compare if its visible in the cron PATH variable:
find /usr -type f -name tmpreaper

the output:

So, the directory is not visible by the crontab, so we have to add it on the crontab (crontab -e):


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: