bash: find and zip all subfolders of a folder

2

1

how would one find all subfolders of a folder and zip them separately ?

folder structure

./folder  
   -subfolder_1  
   -subfolder_2   
   -subfolder_3 

expected result:

./folder
   -subfolder_1.zip
   -subfolder_2.zip
   -subfolder_3.zip

I have tried the following:

for i in .; do zip -r $i.zip $i; done; 

resulted in one ..zip file containing all the subfolders

m1k3y02

Posted 2015-02-10T10:27:02.427

Reputation: 265

Answers

3

Actually solution provided by m1k3y02(for i in *; do zip -r "$i.zip" $i; done) will work only if current directory contains only subdirectories.

Better way for finding and zipping only subdirectories:

for dir in ./* ;do
    if [[ -d $dir ]];then
        zip -r ${dir}.zip $dir
    fi
done

or

find . -type d -maxdepth 1 -exec zip -r {}.zip {} \;

zuberuber

Posted 2015-02-10T10:27:02.427

Reputation: 146

In the second way, -mindepth 1 option is needed to exclude the . folder. – amit – 2019-06-29T15:37:54.653

0

solution as follows...

for i in *; do zip -r "$i.zip" $i; done; 

m1k3y02

Posted 2015-02-10T10:27:02.427

Reputation: 265