crmepham
crmepham

Reputation: 4740

Bash script not deleting files in given directory

I found this bash script online that I want to use to delete files older than 2 days:

#!/bin/bash

find /path/to/dir -type f -mtime +2 -exec rm {} \;

I setup a cronjob to run the script (I set it a couple of minutes ahead for testing, but it should run once every 24 hours)

54 18 * * * /path/to/another/dir/script.sh

I exit correct so it updates the cronjob.

Why does it not delete the files in the directory?

Upvotes: 0

Views: 781

Answers (2)

Lucas
Lucas

Reputation: 10303

What if you try dumping an echo at the end of the script and log the output

cron1.sh >> /var/log/cron1.log  

You could try this but I'm not sure it will work

--exec rm -rf {}

Upvotes: 1

alvits
alvits

Reputation: 6758

Most cron jobs do not have PATH set. You must fully qualify the find command.

#!/bin/bash
/usr/bin/find /path/to/dir -type f -mtime +2 -exec rm {} \;

If you capture the stdout and stderr as recommended by damienfrancois, you'd probably see the message "command not found: find". If you didn't capture the stdout and stderr, cron usually will send the output to the cron job owner's email, unless configured not to do so.

Upvotes: 0

Related Questions