Solved

Cron delete files older than one week old from a folder

Posted on 2013-01-02
5
850 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
  • 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 500 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now