I'm trying to delete files older than six days, then log the files which get deleted.
So far.

In a sh file, I got following;

find /home/pi/ftp/upload -type f -mtime +6 -exec rm {} +

Then within sudo crontab

59 23 * * * /home/pi/scripts/cullftp.sh > /var/log/ftp/`date +\%Y-\%m-\%d-\%H\%M\%S`-cull.log 2>&1

But when it runs at midnight, which only creates a empty file, and none of the files get deleted.

Although this bit of the code work:

find /home/pi/ftp/upload -type f -mtime +6

What is the best way to solve this.

1 Answer 1


1) Make sure the script file is executable, and has a proper hashbang line (#!/bin/sh or #!/bin/bash or such), though you should get an error if it isn't executable.

2) find ... -exec rm will not print anything, you'd need to explicitly tell find to print the filenames too, e.g. find ... -exec rm + -print or find ... -delete -print if your find supports -delete.

3) At least on GNU find, -mtime +6 has some interesting rounding. It first rounds the time down to full days (24 h periods), and then sees if the resulting time is strictly greater than 6. The result is that it only matches files that are at least 7*24 hours old. Using something like -mmin +8640 would lessen the impact. (6 days * 24 h/day * 60 min/h = 8640 min)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .