Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a single directory that contains dozens of directories inside of it.

I'm new to command line and I'm struggling to come up with a command that will zip each sub-directory into a unique sub-directory.zip file.

So in the end my primary directory will be filled with all of my original sub-directories, plus the corresponding .zip files that contain the zipped up content of each sub-directory.

Is something like this possible? If so, please show me how it's done.

share|improve this question
    
I suggest you look up how "for loops" can be done in the shell. There are onliners that can do what you want, but a nice loop will be much clearer. –  lynxlynxlynx Mar 19 '13 at 21:20
    
So does that mean I need to write a separate shell script, and then call that script from the command line? Sorry for such an elementary question. Although I do have some programming experience this stuff is new to me. –  Evster Mar 19 '13 at 21:31

1 Answer 1

up vote 20 down vote accepted

You can use this loop in bash:

for i in */; do zip -r "${i%/}.zip" "$i"; done

i is the name of the loop variable. */ means every subdirectory of the current directory, and will include a trailing slash in those names. Make sure you cd to the right place before executing this. "$i" simply names that directory, including trailing slash. The quotation marks ensure that whitespace in the directory name won't cause trouble. ${i%/} is like $i but with the trailing slash removed, so you can use that to construct the name of the zip file.

If you want to see how this works, include an echo before the zip and you will see the commands printed instead of executed.

share|improve this answer
    
Thanks this looks great! However one potential problem I thought of: will this zip ALL directories within each sub-directory into separate files? I only want to zip the 1st-level subdirectories as single compressed files. I don't want to create additional zip files for 2nd-level, 3rd-level sub-directories, etc... Please let me know if that makes sense. Thanks again! –  Evster Mar 19 '13 at 21:44
    
Only the first level ones like you requested. –  lynxlynxlynx Mar 19 '13 at 21:49
    
The */ names every direct subdirectory, not every descendant directory. So you'll only get zips for the uppermost level, just the way you want them. The version with echo in it would have demonstrated that aspect. Zipping every nested directory into its own file would in fact be more work, and probably best solved using find -type d among other tricks. –  MvG Mar 19 '13 at 21:53
    
UPDATE: I tested it out and it appears to function the way that I want it to (i.e - only zipping 1st-level sub-directories into individual zip files). –  Evster Mar 19 '13 at 21:54
    
You are the man! This is going to be such a great time-saver. Consider this question answered. Thank you! –  Evster Mar 19 '13 at 21:56

protected by Community Nov 2 '13 at 14:37

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.