Mac OS: Zipping with your terminal

The basic command structure for compressing a file into the zip format with the terminal is:

zip -r <destination> <source>

If you want to compress more than one folder or item:

zip -r <destination> <source1> <source2>...

The source can be a file or folder.

Unfortunately zip is not very smart when it comes to folders and it will save not only the file, but the path to the file as well. We can fix this behavior by adding just a little bit to the original command.

Example

Your terminal is set by default to your computer’s home directory. If you want to zip something on your desktop would would have to add that into your command code line, “zip -r Desktop/folder.zip Desktop/folder”. It will create the zip, but inside the folder.zip you will first see a folder called “Desktop”, then inside that folder you would find your target folder, “folder”. You zipped not only the folder, but the path to the folder too. Not ideal.

To get around this behavior, a quick method is to use the command “cd” to change your current directory. Here’s how it looks:

cd Desktop && zip -r folder.zip folder

Here we are connecting the two commands together using double ampersands “&&”, creating a one-line solution.

For more information on how to use the “cd” command, please look to my previous post.

Leave a Reply

Your email address will not be published. Required fields are marked *