Source URL: http://unix.stackexchange.com/questions/68489/command-to-zip-multiple-directories-into-individual-zip-files
Given solution:
You can use this loop inbash
: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 youcd
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 anecho
before thezip
and you will see the commands printed instead of executed.
What i'm using:
for i in */; do zip -rm9 "${i%/}.zip" "$i"; donezip -r : zip recursively
zip -m : zip and remove source file(move target files into zip).
zip -9 : zip with compress better
Steps I did:
- Navigate to target directory: cd /opt/Programs/apache-tomcat-7.0.39.2/webapps/ProjectABC/WEB-INF/classes/archive
- Create bash script file: vi zip_folders.sh
- After enable edit mode by press "i", then paste script and enhance to my requirement: for i in */; do zip -rm9 "${i%/}.zip" "$i"; done
- Exit edit mode by: Press Esc.
- Save file and exit VI editor by: :wq
- Make file "runable" by: chmod +x zip_folders.sh
- then run file by: ./zip_folders.sh