Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress multiple folders, each into its own zip archive?

So I want to create a script that enables me to compresses each folder into its own zip archive and not into a one big zip file.

As an example, I've got a directory:

+ MyDirectory/ | | | + Folder_01/ | | | + Folder_02/ | | | + Folder_03/ | 

When I'm done running the script under MyDirectory then I would have a zip file of each Folder which is inside MyDirectory: Folder_01.zip, Folder_02.zip and Folder_03.zip. I know some BASH but this is something I can't figure out.

How can this be done?

Kind regards.

like image 577
Villi Magg Avatar asked Dec 08 '13 10:12

Villi Magg


People also ask

How do I archive multiple folders?

1: create a new folder, then select folder that you would like to put into archive folder and drag to the new folder, then a prompt box popped out there, click Yes. 2: After you move the 60 folders to the new folder, drag the new folder to the archive Folder.

How do I compress multiple files in multiple archives?

Steps to zip folders into multiple files:Open Winzip. From the WinZip file pane select the file you want to split. Next, click Add to Zip and make sure to select the Split option. Indicate where you want your zip files to be saved.


2 Answers

for i in * do [ -d "$i" ] && zip -r "$i.zip" "$i" done 

You walk through all the directories and create zip for each of them.

Or even more concise:

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

(thanks to damienfrancois for suggestion).

like image 125
Igor Chubin Avatar answered Oct 06 '22 10:10

Igor Chubin


here

for i in */; do tar -czvf "${i%/}.tar.gz" "$i"; done 
like image 43
ahmed zeaad Avatar answered Oct 06 '22 10:10

ahmed zeaad