Compress files from OS X terminal?

57

14

In the Finder, there is this wonderful ability to right click on a file or directory, select compress from the drop-down, and end up with a zipped file.

Is it possible to do the same thing from the terminal?

William Jockusch

Posted 2012-11-13T13:43:57.503

Reputation: 2 913

Answers

78

It's called zip.

This adds the file file to the archive file.zip:

zip file.zip file

Of course, to add more files, just add them as arguments to the command. Check out man zip for more options.

Often, you'll want to skip including those pesky .DS_Store files, for example compressing the whole folder folder into folder.zip:

zip -vr folder.zip folder/ -x "*.DS_Store"

slhck

Posted 2012-11-13T13:43:57.503

Reputation: 182 472

whats the (1) after the word zip? – Jacob Raccuia – 2014-07-28T15:43:22.030

Is it the plain old GNU zip that comes with OS X? – mwfearnley – 2017-11-10T16:10:04.040

@mwfearnley Under macOS, it's Copyright (c) 1990-2008 Info-ZIP. https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/zip.1.html

– slhck – 2017-11-11T09:47:41.200

link to manpage broke .. just use man zip on the command line – commonpike – 2019-05-04T09:51:31.787

16

To compress the files exactly as the Finder command would compress them use:

ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip

See man ditto for details:

 The command:
       ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip
 will create a PKZip archive similarly to the Finder's Compress function-
 ality.

qqbenq

Posted 2012-11-13T13:43:57.503

Reputation: 275

3

This is the best answer because it produces an identical zip, whereas CLI zip or tar is different and slightly smaller. A similar question with the same answer: http://stackoverflow.com/questions/10738505/mac-os-x-compress-option-vs-command-line-zip-why-do-they-produce-different-re

– Henry Blyth – 2017-05-11T15:18:45.133

5

There is tar(1) and gzip (or bzip2 or lzma). Tar is used to roll a number of files into one archive, while the one of the other three is used to compress it.

On a command line, you will call tar with a couple of options to create an archive and gzip it.

E.g.:

tar -c -z -f myarchive.tar.gz -C /home/username Downloads

This willl -c reate a g -z ipped archive named -f ile from the -C hange-folder-to directory and will contain all files in the folder Downloads. The -C option is optional and the source-file arguments will be taken from the current folder if omitted.

For reference: tar tutorial

Ярослав Рахматуллин

Posted 2012-11-13T13:43:57.503

Reputation: 9 076