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??
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!
More Useful Shell Script Programming Articles:
✔ Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...✔ Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...✔ Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...✔ Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...✔ Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+