How to ZIP a Folder from the Mac Terminal (zip, ditto, tar)

Updated

Compressing files in a terminal window

To zip a folder from the Mac Terminal:

zip -r Project.zip Project -x '*.DS_Store'

That is the right answer for sending a folder to someone. It is the wrong answer if the folder contains files with resource forks or extended attributes, because zip throws them away. For that, macOS has its own archiver:

ditto -c -k --sequesterRsrc --keepParent Project Project.zip
ditto -x -k Project.zip restored/

zip -r -X is not how you ensure all hidden files are included. -X is --no-extra; it strips extra file attributes rather than including anything, and it has no effect on hidden files at all. Hidden files are already included, always.

-X changes the size of the archive, not its contents

Zipping the same folder with and without -X and diffing the two file lists:

$ diff <(unzip -Z1 plain.zip | sort) <(unzip -Z1 minusX.zip | sort)
IDENTICAL FILE LISTS

$ wc -c plain.zip minusX.zip
    1692 plain.zip
    1172 minusX.zip

Ten entries in both, including Project/.hidden, Project/.DS_Store and Project/.git/HEAD. zip -r descends into dot-directories and archives dotfiles without being asked. -X saved 520 bytes by dropping uid/gid and timestamp extra fields — useful for reproducible builds, irrelevant to hidden files.

Excluding the noise

zip -r Project.zip Project \
    -x '*.DS_Store' '*/.git/*' '*/.git' '__MACOSX/*'

Quote every pattern. An unquoted -x *.git* is expanded by your shell against the current directory before zip ever sees it, so it works or fails depending on which folder you happen to be standing in. To clean an archive you already have, zip -d Project.zip '__MACOSX/*' deletes entries in place.

What __MACOSX actually is

It is not junk. It is where macOS stores the half of your file that ZIP has no field for. Compare what each tool produces from the same folder, which contains one file carrying a 21-byte resource fork and a custom extended attribute:

zip -r          →  10 entries, no __MACOSX
ditto -c -k     →  19 entries, AppleDouble stored inline as Project/._tagged.txt
ditto -c -k --sequesterRsrc
                →  23 entries, AppleDouble moved to __MACOSX/Project/._tagged.txt

--sequesterRsrc is what Finder’s “Compress” does, which is why Mac-made zips arrive on Windows with a stray __MACOSX folder. Now the part that matters — extracting each archive and checking whether the metadata survived:

zip -r      → unzip   → resource fork: gone.   com.example.note: gone.
ditto -c -k --sequesterRsrc → ditto -x -k
                             → resource fork: 21 bytes, "RESOURCE FORK PAYLOAD"
                               com.example.note: "important"
ditto archive → plain unzip → resource fork: gone, and a literal
                              __MACOSX/ directory left on disk

So: ditto -c -k --sequesterRsrc is a lossless round trip only if the other end also uses ditto -x -k. Hand the same archive to unzip and you get the metadata as visible clutter instead of as metadata. Deleting __MACOSX from an archive you made with ditto discards the thing ditto was preserving; deleting it from an archive you received costs you nothing you can use.

The surprise: tar is the cleanest option on macOS

tar -czf Project.tgz Project
tar -xzf Project.tgz -C restored/

macOS ships bsdtar, which stores Apple metadata as pax extended headers. Round-tripping the same test folder:

resource fork after extract:  21 bytes ("RESOURCE FORK PAYLOAD")
com.example.note:             "important"
archive listing:              10 entries — no __MACOSX, no ._ files

Full fidelity, no sidecar entries, smaller output. COPYFILE_DISABLE=1 tar -czf … turns the metadata off; on this test folder that took the archive from 1190 down to 881 bytes (gzip output shifts by a few bytes run to run, so expect close rather than identical figures). The catch is portability: those pax headers are a macOS convention, and GNU tar on Linux will surface them as ._ files rather than reattaching them. That specific claim is not tested here, but it is the documented behaviour and the reason tar is a Mac-to-Mac answer, not a universal one.

Password protection: zip -e is not encryption you should rely on

Info-ZIP says so itself. From zip -h2 on the copy Apple ships:

Encryption:
  -e        use standard (weak) PKZip 2.0 encryption, prompt for password
  -P pswd   use standard encryption, password is pswd

That is ZipCrypto, a 1990 stream cipher with a published known-plaintext attack: if an attacker can guess or obtain roughly 12 bytes of any file in the archive, they can recover the keystream and decrypt the rest without your password. This zip has no AES option — the help text lists none, and Apple’s build is Info-ZIP 3.0 from 2008.

It also does not hide what you are protecting. The password guards file contents only:

$ unzip -Z1 enc.zip          # no password supplied
Project/
Project/.hidden
Project/.DS_Store
Project/tagged.txt

Every path, every filename, every file size, readable by anyone. And do not use -P outside a throwaway test — the password sits in your shell history and in the process list for any user on the machine to read with ps. -e prompts instead, which is the point of it.

For anything real, encrypt the tarball with a modern cipher and a real key-derivation function:

tar -czf - Project | openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt \
     -out Project.tgz.enc

openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in Project.tgz.enc | \
     tar -xzf - -C restored/

Verified end to end: content restored intact, the 21-byte resource fork survived, and grepping the ciphertext for a filename in the archive returns zero matches — unlike zip -e, the names are inside the encrypted blob. A wrong password fails loudly with bad decrypt and exit status 1.

Two smaller things worth measuring

-9 is usually not worth typing. On 69 real HTML files totalling 7948 KB:

-0  8,026,604 bytes
-1  2,225,682
-6  1,915,443   (this is the default)
-9  1,909,986   (0.28% smaller than the default)
tar.gz 1,897,172

Split archives are not readable until reassembled. zip -r -s 200k split.zip Big produced split.z01, split.z02 and split.zip. Pointing unzip -t at split.zip fails with bad zipfile offset (local header sig). Join them first, then test:

zip -s 0 split.zip --out full.zip
unzip -t full.zip        # No errors detected in compressed data of full.zip.

Everything above ran on macOS 26.6 (arm64) against Zip 3.0 (Info-ZIP, Apple build), UnZip, ditto, bsdtar 3.5.3 and OpenSSL 3.6.3. The only untested claim is how GNU tar on Linux handles macOS pax metadata, flagged as such where it appears.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.