• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 870
  • Last Modified:

Cron delete files older than one week old from a folder

Hi,

I have a folder:

pagecache/pagecache/

that has cached filed !! these are not deleted after any time and are often search pages etc that are never used again, is there a CRON command I can use that will delete any files older than a week (I will run this every night)

I am running the site on a Plesk based server running linux/apache

Thanks
0
tonypearce
Asked:
tonypearce
  • 2
  • 2
1 Solution
 
woolmilkporcCommented:
Let's say you want to run the job at 2:00 AM:

0 2 * * * /usr/bin/find /path/to/pagecache/pagecache -type f -mtime +7 | xargs rm >/dev/null 2>&1

Take care to specify the full path to the pagecache directory!

Put the above line into the crontab of an authorized user (or root's crontab) by means of the "crontab" utility ("man crontab") or by means of Plesk tools:
http://www.hosting.com/support/plesk/crontab
0
 
farzanjCommented:
You can use the find command and schedule it via cron

0 22 * * * find /path/pagecache/pagecache/ -type f -mtime +15 | xargs rm -f
0
 
tonypearceAuthor Commented:
Thanks very much, will this actually 'do' the delete as well?
I will test VERY carefully!
0
 
woolmilkporcCommented:
For testing you can add an "echo" statement, so you will only see the generated commands, but they won't be executed.

Command line:

/usr/bin/find /path/to/pagecache/pagecache -type f -mtime +7 2>/dev/null | xargs echo rm

Cron using a log file:

0 2 * * * /usr/bin/find /path/to/pagecache/pagecache -type f -mtime +7 2>/dev/null | xargs echo rm >/path/to/logfile 2>/dev/null

"xargs" passes as many arguments as possible to "rm" so you will see many arguments (filenames) per "rm" command.

To make the output better readable you could add the flag "-n1" to xargs so you will see one "rm" per file.

/usr/bin/find /path/to/pagecache/pagecache -type f -mtime +7 2>/dev/null | xargs -n1 echo rm
0
 
tonypearceAuthor Commented:
works fine, thinks a lot
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now