Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I rotate files in Unix?

On Solaris I have cron job which creates some log files and it puts it under a directory, this directory name is created based on system date for example directory name are like 08162005, 08172005 so on..

What I need is I want to do a rotation for these log directories every 14 days since these are directories with system dates, I am having little trouble getting this done,

Is there any way you can solve this for me??


Dave's Answer:

If you're trying to figure out how to do date math, then this is going to seem like a very difficult task. But, in fact, what you're talking about is a perfect job for one of the Swiss Army Knives of the Unix command line: find.

Specifically, find has a flag -ctime which lets you test how long ago the file was created. The notation that the program uses for specifying dates is a bit confusing, but I'll give you a shortcut: "-ctime" lets you specify the number of days, so the command "-ctime 14" sounds right, but it actually will only match files that are exactly fourteen days old.

What we want instead, however, is "fourteen days or older", which is done by prefacing the number with a "+".

Put everything together with the peculiar syntax of find and, if you're looking for files in the directory "/var/log/test", you could use:

find /var/log/test -ctime +14 -print

That'll work to identify these files, but what you're actually asking me is about directories that have this naming scheme, and it turns out that you can add another constraint to find that'll limit its results to just directories.

For this, I'm going to assume that your directories all live under the "/var/log/test" directory, but that's easily changed, of course.

To constrain results to just directories, use -type d ("-type f" is just files, for example). Put it together and:

find /var/log/test -ctime +14 -type d -print

That should identify the files you want. To automatically delete them, use "xargs" thusly:

find /var/log/test -ctime +14 -type d -print |
xargs /bin/rm -f

That should do exactly what you seek!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

One question. Can't you leave the "-print" arg off? It seems to default to print in all environments I've used (Solaris, Fedora, cygwin).

Erik

Posted by: Erik Weibust at August 29, 2005 5:59 PM

You're correct, Erik. I'm just an old-fashioned purist about this stuff. :-)

Posted by: Dave Taylor at August 29, 2005 7:09 PM

Erik-

I would suggest that the most portable way to write the script is to use the -print option to expressly state your intent.

If you assume that it works in all environments, it may behave unexpectedly.

We always use -print even if it is the default because we have multiple versions of OS's and not all of them support it by default.

It makes it easier to develop scripts this way.

Posted by: Jim at May 7, 2007 8:32 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]