Skip to content

Zipping & Unzipping

There’s an old comic about defusing a bomb by typing a valid tar command from memory. It’s funny because it’s true. Here’s the cheat sheet so you don’t have to defuse anything.

tar bundles files into one archive. Compression is a separate layer added by a flag. The letters you care about:

Flag Means
c create
x extract
t list contents (look before you leap)
f file. The archive name comes right after it.
v verbose. Prints every file.
z gzip (.tar.gz / .tgz)
J xz (.tar.xz). Smaller, slower.
--zstd zstd (.tar.zst). Fast and small. Use this when you can.
Terminal window
# Create
tar czf site-backup.tar.gz /var/www/example.com
tar --zstd -cf logs.tar.zst /var/log/myapp/
# List (always do this with archives from strangers)
tar tzf site-backup.tar.gz | head
# Extract here
tar xzf site-backup.tar.gz
# Extract somewhere else
tar xzf site-backup.tar.gz -C /tmp/restore/
# Extract one file
tar xzf site-backup.tar.gz var/www/example.com/index.html

Modern GNU tar auto-detects compression when extracting, so tar xf anything.tar.* just works. You only need z/J/--zstd when creating.

tar czf backup.tar.gz /var/www/example.com stores paths as var/www/example.com/... (tar strips the leading / and tells you so). Extract it and you get a var/www/example.com/ tree in your current directory, not your files in place. That’s tar protecting you from yourself.

To archive a directory’s contents without the full path:

Terminal window
tar czf site.tar.gz -C /var/www/example.com .

-C means “cd here first.” It’s the flag that keeps archives tidy.

Terminal window
tar czf app.tar.gz --exclude='node_modules' --exclude='.git' --exclude='*.log' -C /opt app

When run as root, tar preserves ownership on extract by default. As a normal user, files end up owned by you. For backups you plan to restore as root, create and extract as root (sudo), and add -p to keep permissions exact.

For compressing one file, like a log or a SQL dump. No tar needed:

Terminal window
gzip big.log # -> big.log.gz, and the original is DELETED
gzip -k big.log # keep the original
gunzip big.log.gz # decompress
zcat big.log.gz | grep ERROR # read without decompressing
zgrep ERROR big.log.gz # same, shorter
zstd big.sql # -> big.sql.zst, original kept by default
unzstd big.sql.zst
zstdcat big.sql.zst | less

Compress on the fly in a pipe. A dump that never touches the disk uncompressed is a dump that won’t fill the disk:

Terminal window
pg_dump mydb | zstd > mydb-$(date +%F).sql.zst
zstdcat mydb-2026-11-26.sql.zst | psql mydb

zip / unzip: for talking to Windows people

Section titled “zip / unzip: for talking to Windows people”

Use zip when the file is going to someone on Windows or macOS who’ll double-click it. Otherwise use tar. zip doesn’t preserve Unix permissions or ownership reliably.

Terminal window
sudo apt install zip unzip # not always installed. Of course it isn't.
zip -r site.zip site/ # -r or you get an empty-ish zip with just the folder name
zip -r site.zip site/ -x '*.git*' '*node_modules*'
zip -e -r secret.zip docs/ # password-protected (weak legacy crypto, it's a speed bump)
unzip -l site.zip # list
unzip site.zip # extract here
unzip site.zip -d /tmp/out # extract somewhere else
unzip -o site.zip # overwrite without asking
Terminal window
7z x archive.7z # 7-Zip. Also opens most other formats, including .rar.
unxz file.xz
bunzip2 file.bz2

If you get a .rar, someone is testing you. 7z x handles it.

Situation Use
Backup a directory on Linux tar --zstd -cf (or tar czf for compatibility)
Send to a Windows user zip -r
Compress one log/dump zstd or gzip
Maximum compression, don’t care about time tar cJf (xz)
Archive from a stranger tar tf / unzip -l first