LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-01-2012, 12:46 PM   #1
Znrall
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
Bash Question(for loop): How to Zip multiple files under multiple directories


So im trying to learn Bash and I figured out how to do a very simple for loop.

But I can't figure out this slightly more complex one.

My logs are stored in a directory structure like this. /<processname>/<date>/*.logs

I want to zip each individual date directory into its own zip with a name like $processname.date.zip

What I was doing is pretty cumbersome. I was moving all the <date> directory to a new subdirectory called 'zipdrive' and then it was only naming it with the date. I was doing this:

for i in *; do zip -r zipdrive/$i.zip $i ; done

There has to be a much faster way to do this, can anyone help?

Bonus question: How would I specify particular date ranges to be zipped?

Last edited by Znrall; 08-01-2012 at 12:47 PM.
 
Old 08-01-2012, 01:24 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Ok, well before you go and re-invent the wheel lets look at log rotate as this will do this functionality for you. I'm sure you've seen some of your system log files that are compressed with a .1 .2 .3 and so on extension. This is the log rotate daemon compressing and renaming old log files for you.

To answer your original question, yes this can be accomplished through a for loop or a more intricate script via a cron job or the like but the number one rule for being a good sys admin is to use the right tool for the job.


Read over this tutorial: http://www.thegeekstuff.com/2010/07/logrotate-examples/

This will give you an idea of how to implement your solution, if you have further questions we can help you at that point. And if you are unable to use log rotate for this then we can get back to your original train of thought and try to help you write a decent script you can put into a cron job.
 
Old 08-01-2012, 02:52 PM   #3
Znrall
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you for that. Quite interesting little piece of software. I will read up on that for some home use.

What I was trying to do was just a small one-off log zipping. The system already has a perl script that grabs everything and puts it where it goes. I just needed a small subset of those files zipped.

I eventually did it by modifying the line i had in there. To manually add the process name, and then I just had it make the zips in the base folder.

for i in 2011*; do zip -r processname1.$i.zip $i ; done

That seemed to work out ok, I only had about 20 processes that this had to be run on. Thank you for your help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash quesion - scp on multiple hosts to multiple directories in each one mierea.ciprian Programming 2 06-22-2012 01:27 PM
cp multiple files to multiple directories Heksie Linux - Newbie 2 02-05-2009 06:17 AM
Zip multiple directories to seperate archives PlymWS Linux - Software 3 06-16-2007 05:40 AM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 09:16 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 06:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration