2

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?

  • Do you want this for just one folder depth level? I mean, is there another folder in subfolder1 ? Also have you seen "create archive action" which " creates a zip format archive (.zip) from the files specified." Please post your automator workflow. apple.stackexchange.com/questions/63428/… – ankii Dec 30 '19 at 13:23
4

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.
|improve this answer|||||
  • 1
    Or to also recurse could add another loop something like for d in $(find . -type d); do; for f in $(find $d -type f -depth 1); do; zip -r "${d}/${d##*/}.zip" "${f}"; done; done (I copied the ${d##*/} bit from here) – lx07 Dec 30 '19 at 15:19
  • @lx07 Thanks for reminding me of -depth :-) – nohillside Dec 30 '19 at 15:21
  • 2
    Nice use (again) of -execdir. +1 – user3439894 Dec 30 '19 at 19:57
  • 2
    @nohillside, this could be the most useful answer I got on StackExchange. I mean it. – turzifer Dec 31 '19 at 7:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .