?
Solved

Cron delete files older than one week old from a folder

Posted on 2013-01-02
5
Medium Priority
?
860 Views
Last Modified: 2013-01-08
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
Comment
Question by:tonypearce
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38736823
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
 
LVL 31

Expert Comment

by:farzanj
ID: 38736835
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
 

Author Comment

by:tonypearce
ID: 38739652
Thanks very much, will this actually 'do' the delete as well?
I will test VERY carefully!
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 38739662
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
 

Author Comment

by:tonypearce
ID: 38756386
works fine, thinks a lot
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question