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: the one you’ll use most
Section titled “tar: the one you’ll use most”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. |
# Createtar czf site-backup.tar.gz /var/www/example.comtar --zstd -cf logs.tar.zst /var/log/myapp/
# List (always do this with archives from strangers)tar tzf site-backup.tar.gz | head
# Extract heretar xzf site-backup.tar.gz
# Extract somewhere elsetar xzf site-backup.tar.gz -C /tmp/restore/
# Extract one filetar xzf site-backup.tar.gz var/www/example.com/index.htmlModern GNU tar auto-detects compression when extracting, so tar xf anything.tar.* just works. You only need z/J/--zstd when creating.
Paths inside the archive
Section titled “Paths inside the archive”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:
tar czf site.tar.gz -C /var/www/example.com .-C means “cd here first.” It’s the flag that keeps archives tidy.
Excluding junk
Section titled “Excluding junk”tar czf app.tar.gz --exclude='node_modules' --exclude='.git' --exclude='*.log' -C /opt appKeeping permissions
Section titled “Keeping permissions”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.
gzip / zstd on a single file
Section titled “gzip / zstd on a single file”For compressing one file, like a log or a SQL dump. No tar needed:
gzip big.log # -> big.log.gz, and the original is DELETEDgzip -k big.log # keep the originalgunzip big.log.gz # decompresszcat big.log.gz | grep ERROR # read without decompressingzgrep ERROR big.log.gz # same, shorter
zstd big.sql # -> big.sql.zst, original kept by defaultunzstd big.sql.zstzstdcat big.sql.zst | lessCompress on the fly in a pipe. A dump that never touches the disk uncompressed is a dump that won’t fill the disk:
pg_dump mydb | zstd > mydb-$(date +%F).sql.zstzstdcat mydb-2026-11-26.sql.zst | psql mydbzip / 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.
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 namezip -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 # listunzip site.zip # extract hereunzip site.zip -d /tmp/out # extract somewhere elseunzip -o site.zip # overwrite without askingOther formats you’ll get handed
Section titled “Other formats you’ll get handed”7z x archive.7z # 7-Zip. Also opens most other formats, including .rar.unxz file.xzbunzip2 file.bz2If you get a .rar, someone is testing you. 7z x handles it.
Which one, quickly
Section titled “Which one, quickly”| 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 |