Solved

Cron delete files older than one week old from a folder

Posted on 2013-01-02
5
849 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

760 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

20 Experts available now in Live!

Get 1:1 Help Now