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

Viewed 26285

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.

5 Answers

here

for i in */; do tar -czvf "${i%/}.tar.gz" "$i"; done

I take the old solutions and get back with another good solution that works for any directory in any place in you drive

This code in Bash

#!/bin/bash

folderPath=/home/ahmed/test/

for folder in $folderPath*
do
        folderName=$(basename $folder)
        cd $folderPath && zip -r $folderName.zip $folderName
done

So all folders inside test folder will be zipped in the same folder

I personally use

fd . -t d -x zip -r '{}.zip' '{}'

(fd might be fd-find depending on your OS)

Related