Terminal: Compressing multiple folders

Termianl-IconYou have a directory full of important folders, and you want each one compressed separately. Doing this by hand would take waaay to much time. Of course, we can do this in terminal, and with one line of code (sweet!) so it’s easy to use.

For this we use the “Find” command built in into our computer. From there we’ll have two choices, to use compress it to a zip file for a dmg file.

The basic find command for getting only folders is in our current directory is:

find . -type d -d 1

at the end we can add what we want to do with the results, marked as {} in our code:

Create Zip Folders:

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

Create DMG Images:

find . -type d -d 1 -exec hdiutil create -format UDZO -srcfolder {} {}.dmg \;

Just copy and paste it into your terminal.

Make It A Clickable App

If you want this same functionality but in as a clickable app, we can do this in Applescript. Below is the applescript code that will do the able compression in a set directory. Replace the directory with the one you want to use (just drag and drop it into the script window) and change the ending of the last script to match the kind of compression you want (cmd_zip or cmd_dmg):

set the_dir to "/Users/me/Desktop/MyFiles/"
set zip_cmd to "find . -type d -d 1 -exec zip -r {} {} \\;"
set dmg_cmd to "find . -type d -d 1 -exec hdiutil create -format UDZO -srcfolder {} {}.dmg \\;"

do shell script "cd '." & the_dir & "' && " & dmg_cmd

One thought on “Terminal: Compressing multiple folders

Leave a comment