When I right-click a file in Mac OSX and select "Compress 'File Name'", what is the equivalent bash or terminal command to get the same result?

Is it just gzip -9 /path/to/filename?

3 Answers 3

ditto -ck --rsrc --sequesterRsrc --keepParent folder folder.zip

ditto -ck uses Info-ZIP's zip.


gzip is a different, incompatible algorithm. It's better compression, and not encumbered by any patents, but it's only supported out of the box on Unix and Unix-like systems (including Linux and OSX, but not Windows). What you're looking for is zip. OSX ships with that as a command-line utility too. See man zip for options.

  • +1 Not encumbered by any patents, nice.
    – squiguy
    Mar 28, 2013 at 22:32
  • The command-line zip (and the Compress menu item) use the same compression method as gzip, i.e. deflate. The "better compression" you're thinking of could only be with respect to compressing a tar file as one thing with gzip, as opposed to the zip approach of compressing each file individually. In any case, both gzip and Info-ZIP zip are not encumbered by any patents.
    – Mark Adler
    Mar 29, 2013 at 0:15

If you 'Compress' a directory/folder called myDir, or something like that, then you get the equivalent of

% zip -r myDir.zip myDir

If you 'Compress' a single file, f1, then you get something like

% zip -r f1.zip f1

This is Info-ZIP; there's a long and painful story about Info-ZIP vs PKZIP, which has patents and personalia aplenty. As far as I'm aware, Info-ZIP is unencumbered.

You can unpack Zip archives with unzip, and list their contents with unzip -l.

Just by the way, one important difference between Zip and gzip is that Zip is both an archiver and a compressor, whereas gzip is designed only to compress single files. The files that gzip compresses might be archive files (most commonly tar files), but the two jobs are separate. If you want really good compression, then bzip2 (see the man page) is reported to be more effective even than gzip -9.

  • 2
    The zip command doesn't include anything beyond basic file metadata in the archive, while the Finder includes them using the AppleDouble format. To include the metadata, use ditto instead (see @Mark Adler's answer). Mar 29, 2013 at 1:26
  • Ahaaa: thank you. I was sure there had to be some difference; I should have thought of looking at ditto. It's good to know that the difference concerns only AppleDouble and resources. Mar 29, 2013 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.