View Single Post
Old September 16th, 2013, 02:20 PM   #918 (permalink)
MoodyBlues
- Crazy peacock person -
Thread Author (OP)
 
MoodyBlues's Avatar
 
Join Date: Jan 2011
Location: echo $HOME; California
Posts: 2,782
 
Device(s): Kindle Fire HD 8.9" (rooted), Motorola Atrix 2, Motorola Atrix 4G (retired), Motorola Bravo (retired
Carrier: AT&T

Thanks: 927
Thanked 1,363 Times in 822 Posts
Default

Quote:
Let's say I have a directory that contains subdirectories that I would like to zip in such a way that each subdirectory has its own zip file. Is there a way to do this with a script? I know I could do this manually but I would rather be able to just run the script

What I have so far:

[HIGH]#!/bin/bash
WORK=/path/to/work/directory
SUBDIR=$1
ls -l $WORK/$SUBDIR > ~/listing
list="cat ~/listing | wc -l"
for d in $list; do
zip -r $d.zip $WORK/$SUBDIR/$d
done[/HIGH]

Something's obviously wrong with this though as it doesn't do what I want it to do
I just slapped something together, but I'm not 100% sure it's exactly what you're after.

From your snippet, it looks like you want to hard-code the working directory, then at runtime you want to manually enter the name of a subdirectory to zip, correct? Is that actually how you want it? If so...ignore the following!

This--as it stands right now--has nothing hard-coded, meaning it can be run in any directory, and it automatically finds all of its subdirectories, and zips each one as its own subdirectory-named zip file in its own subdirectory. Obviously, this can be tweaked as far as location of the zipped files, whether or not you enter a directory/subdirectory name, and so on:

Code:
#!/bin/bash
for SUBDIR in `ls -d1 */` 
do ZIPFILE=`basename $SUBDIR`
zip -r $SUBDIR$ZIPFILE.zip $SUBDIR
done
Any questions about what's doing what, just ask.
__________________
Info on Heartbleed bug; updated with helpful links


MoodyBlues is offline  
Reply With Quote
The Following User Says Thank You to MoodyBlues For This Useful Post:
palmtree5 (September 16th, 2013)