Solved

Cron delete files older than one week old from a folder

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Like to learn Linux, need advice for a good book and setup. 3 58
ifconfig 4 69
VMware Tools Install On Linux Problem 3 84
Migrating a Linux server to VMware 3 59
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

828 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