How to compress multiple folders into multiple zip files

bashzip

My folder structure is:

YEAR – MONTH – DAY – SUBFOLDERS

I want to compress each subfolder in the [DAY] directories. The solution from here which is:

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

works fine as long as I'm doing it from a [DAY] folder. But if I change it to:

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

and run from a [MONTH] folder it still works, but the problem is that the .zip now contains an additional parent folder for each additional '/*' in the command when I unpack.

How can I run the command from a [YEAR] folder with '/* /* /*' while making sure that the .zip only contains the lowest level subfolder when I unpack?

Best Answer

In your case you can combine two loops to accomplish this

cd YEAR
for month in */; do
    (cd "$month";  for i in */; do zip -r "${i%/}.zip" "$i"; done)
done

PS: If MONTH folders can be empty you need to check for this in the inner loop.