Zip the contents of every sub-folder in a folder in Mac automatically

I have this folder/file structure

parent folder
--subfolder1
  -sub-subfolder
  -file1
  -file2
--subfolder2
  -sub-subfolder
  -file1
  -file2
...

I want to:

  • create zip files from the contents of the subfolders. which means, file1, file2, and sub-subfolder will be zipped, but not the enclosing subfolder.

  • name each zip file with the name of the folder, like subfolder1.zip, subfolder2.zip...

I'd prefer that the zip files remain in the related subfolder so it will be:

parent folder
--subfolder1
  -sub-subfolder
  -file1
  -file2
  -subfolder1.zip
--subfolder2
  -sub-subfolder
  -file1
  -file2
  -subfolder2.zip
...

I am hoping that it would be possible with Automator but had no success till now.

My searches resulted in solutions that zip every folder individually with the folder itself, which in this case is not useful for me. I need the contents of the folders to be zipped.

Any ideas?


Not sure about Automator, but you can easily accomplish this in Terminal.

find "parent folder" -type d -depth 1 -execdir sh -c 'cd "$1" && zip -r "$1" .' _ '{}' \;
  • find "parent folder" -type d -depth 1 lists all subdirectories of "parent folder" (just run it to see the result)
  • -execdir executes the part up to the \; for each subdirectory found
  • sh -c 'cd "$1" && zip -r "$1" .' _ '{}' starts a subshell to change into the directory to pack and then zip the content there. The {} contains the name of the subdirectory and is the value $1 will be set to within the subshell, _ is syntactically required but the actual content is ignored.