bash: find and zip all subfolders of a folder

Solution 1:

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 {} \;

Solution 2:

Here are a couple of tweaks on one of zuberruber's answers:

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

It adds a -mindepth test to avoid zipping the current directory, and it puts the global depth tests before the -type test to avoid a warning. Also, I added --move --test to the zip command so that the unzipped files get removed after successfully being compressed.