15
Archives & Compression
- Tar a directory for handoff
- Extract release artifact
- gzip single file vs tar bundle
Must-know cold
tar -czvf name.tar.gz dir/create ·tar -xzvf name.tar.gzextract ·tar -tzvf name.tar.gzlistgzip file→file.gz·gunzip file.gztarbundles;gzipcompresses one stream/file- Prefer
-t(list) before extract on untrusted archives zip -r out.zip dir/·unzip out.zip
tar dash habit
Definition: Clustered tar flags (
cxvtzf) and why-fmust precede the archive name.
Classic clustered flags:
ccreatexextracttlistvverboseffilezgzipjbzip2Jxz
Cluster habit: put f last in the bundle so the next word is the archive: tar -czvf out.tgz dir/.
tar -czfv out.tgz dir/ makes the archive name v. On GNU tar, a separate -f out.tgz may appear anywhere.
Commands
tar
Definition: Create or extract archive files (often combined with gzip/xz).
| Option | Argument | Meaning | Example |
|---|---|---|---|
-c | — | Create archive | tar -cf a.tar dir/ |
-x | — | Extract archive | tar -xf a.tar |
-t | — | List contents | tar -tf a.tar |
-f | ARCHIVE | Archive file name (required for file ops) | tar -tf a.tar |
-v | — | Verbose; list files processed | tar -cvf a.tar dir/ |
-z | — | gzip compress/decompress | tar -czf a.tar.gz dir/ |
-j | — | bzip2 | tar -cjf a.tar.bz2 dir/ |
-J | — | xz | tar -cJf a.tar.xz dir/ |
-a | — | Auto-compress from suffix (GNU) | tar -caf a.tar.gz dir/ |
-C | DIR | chdir before the next path (create or extract). Prefer this over archiving /absolute paths | tar -czf a.tgz -C /etc myapp |
-p | — | Restore modes exactly (non-root otherwise applies umask) | tar -xpzf a.tgz |
--same-owner | — | Preserve ownership if root | Extract as root |
-h | — | Follow symlinks (store target) | tar -chf a.tar link |
--exclude | PAT | Skip pattern | tar -czf a.tgz --exclude='.git' dir |
--exclude-from | FILE | Exclude list | |
-r | — | Append files to uncompressed tar | Rare |
-u | — | Append only newer | Rare |
--strip-components | N | Strip N leading path components on extract | tar -xzf a.tgz --strip-components=1 |
-k | — | Don’t overwrite existing on extract | tar -xkzf a.tgz |
--overwrite | — | Overwrite (default often) | |
-W | — | Verify after write (GNU) | |
--numeric-owner | — | Use UIDs not names | Portable archives |
-S | — | Handle sparse files | VM images |
--checkpoint | N | Progress every N records | Large archives |
-I / --use-compress-program | CMD | Custom compressor (GNU). Newer tar also has --zstd | tar -I zstd -cf a.tar.zst dir/ |
Flag combos
| Combo | Meaning | Example |
|---|---|---|
tar -czvf out.tar.gz dir/ | Create gzip tarball verbose | Standard handoff |
tar -xzvf out.tar.gz | Extract gzip tarball | |
tar -tzvf out.tar.gz | List gzip tarball | Inspect first |
tar -xzvf out.tar.gz -C /opt/app | Extract into directory | Deploys |
tar -czf out.tgz --exclude='.git' --exclude='node_modules' proj/ | Lean archive | |
tar -cJf out.tar.xz dir/ | xz (smaller, slower) | |
tar -xvf out.tar --strip-components=1 -C /dest | Flatten top folder | Release tarballs |
stdin/stdout:
tar -czf - dir/ > out.tgzcat out.tgz | tar -xzf - -C /dest
gzip
Definition: Compress/decompress a single file with gzip;
zcatreads without unpacking.
| Command | Argument | Meaning | Example |
|---|---|---|---|
gzip | FILE | Compress FILE → FILE.gz (removes original) | gzip big.log |
gzip -k | FILE | Keep original (GNU) | gzip -k big.log |
gzip -c | FILE | Write compressed to stdout | gzip -c f > f.gz |
gzip -d | FILE.gz | Decompress (same as gunzip) | gzip -d f.gz |
gzip -n | — | Don’t save original name/timestamp | |
gzip -N | — | Save/restore name | |
gzip -1 … -9 | — | Speed vs ratio (1 fast … 9 best). Default is -6, not 9 | gzip -9 f |
gzip -t | FILE.gz | Test integrity | gzip -t f.gz |
gzip -v | — | Verbose | gzip -v f |
gunzip | FILE.gz | Decompress | gunzip f.gz |
gunzip -c | FILE.gz | Decompress to stdout | gunzip -c f.gz | less |
zcat | FILE.gz | Decompress to stdout | zcat f.gz | grep ERR |
zgrep | PAT FILE.gz | grep inside gz (if installed) | zgrep ERROR f.gz |
Flag combos
| Combo | Meaning | Example |
|---|---|---|
gzip -k file | Compress keep source | |
zcat app.log.gz | tail | Peek without full extract | |
| vs tar | gzip alone = one file; multi-file → use tar |
bzip2
Definition: Alternative compressors (often via
tar -j/tar -J). Includesxz.
| Command | Argument | Meaning | Example |
|---|---|---|---|
bzip2 | FILE | Compress → .bz2 and delete the original (like gzip) | bzip2 f |
bunzip2 | FILE.bz2 | Decompress | bunzip2 f.bz2 |
bzcat | FILE.bz2 | stdout | bzcat f.bz2 |
xz | FILE | Compress → .xz and delete the original (-k to keep) | xz f |
xz -d / unxz | FILE.xz | Decompress | xz -d f.xz |
xzcat | FILE.xz | stdout | xzcat f.xz |
xz -t | FILE.xz | Test | xz -t f.xz |
With tar: -j = bzip2 · -J = xz.
zip
Definition: Create or extract ZIP archives (common for cross-platform handoff).
| Command | Argument | Meaning | Example |
|---|---|---|---|
zip | OUT.zip FILE... | Create zip | zip out.zip a.txt b.txt |
zip -r | OUT.zip DIR | Recursive directory | zip -r out.zip dir/ |
zip -e | OUT.zip ... | Encrypt (prompt). Default is weak ZipCrypto, not AES | Careful sharing |
zip -u | OUT.zip ... | Update existing | |
zip -q | — | Quiet | |
unzip | ARCHIVE.zip | Extract here | unzip out.zip |
unzip -l | ARCHIVE.zip | List | unzip -l out.zip |
unzip -d | DIR ARCHIVE | Extract into DIR | unzip -d /opt out.zip |
unzip -o | ARCHIVE | Overwrite without prompt | Scripts |
unzip -n | ARCHIVE | Never overwrite | Safe |
unzip -t | ARCHIVE | Test | unzip -t out.zip |
unzip -v | ARCHIVE | Verbose list | |
zipinfo | ARCHIVE | Detailed listing | If installed |
Flag combos
| Combo | Meaning | Example |
|---|---|---|
zip -r release.zip dist/ | Windows-friendly handoff | |
unzip -l release.zip | Inspect first | |
unzip -d /opt/app release.zip | Controlled extract path |
7z
Definition: High-ratio archive tool for 7z and many other formats. Optional — install if missing.
| Option | Argument | Meaning | Example |
|---|---|---|---|
a | OUT SRC | Add to archive | 7z a out.7z dir/ |
x | ARCHIVE | Extract with directory paths | 7z x out.7z |
e | ARCHIVE | Extract flat (drops folders) | Easy to mix up with x |
l | ARCHIVE | List | 7z l out.7z |
t | ARCHIVE | Test | 7z t out.7z |
Common recipes
| Goal | Command |
|---|---|
| Backup dir to tarball | tar -czvf backup-$(date +%F).tar.gz -C /etc myapp |
| Extract into /opt | sudo tar -xzvf app.tar.gz -C /opt |
| List first | tar -tzvf app.tar.gz | head |
| Exclude junk | tar -czf src.tgz --exclude='.git' --exclude='*.o' src/ |
| Compress single log | gzip -k app.log |
| Read gz log | zcat app.log.gz | less or zgrep ERROR app.log.gz |
| Zip project | zip -r project.zip project/ -x '*.git*' |
| Unpack zip | unzip -d /tmp/out project.zip |
| Remote note | Combine with scp / rsync (sheet 16) after create |
Pitfalls
- In a flag cluster, put
flast:tar -czvf out.tgz dir.tar -czfv out.tgz dirnames the archivev. GNU tar accepts a separate-f out.tgzanywhere. - Archiving
/etc/myappstores that path in the tarball. Prefertar -czf a.tgz -C /etc myapp. - GNU tar strips leading
/and..unless-P/--absolute-names. The usual “tarbomb” is an archive with no single top folder that dumps files into cwd. Always-tfirst; extract in an empty dir. - Extract as root restores owners (
--same-owneris the default for root) — check ownership. Non-root extract applies umask unless-p. gzip/bzip2/xz FILEdelete the original unless-k. Default gzip level is-6, not 9.zip -eis ZipCrypto (weak).7z xkeeps paths;7z eflattens them.- Zip vs tar.gz: zip is better for random access / Windows; tar.gz is the Unix tree + permission classic.
- Compressing already-compressed artifacts (jpg, mp4,
.zip) wastes CPU.
For more details, try man <command> in your terminal.