Kiet Nguyen logo
NotesNotesResumeResume
© 2026 Kiet Nguyen
← All categories

15

Archives & Compression

  • Tar a directory for handoff
  • Extract release artifact
  • gzip single file vs tar bundle
targzipbzip2zip7z

Must-know cold

  • tar -czvf name.tar.gz dir/ create · tar -xzvf name.tar.gz extract · tar -tzvf name.tar.gz list
  • gzip file → file.gz · gunzip file.gz
  • tar bundles; gzip compresses 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 -f must precede the archive name.

Classic clustered flags:

  • c create
  • x extract
  • t list
  • v verbose
  • f file
  • z gzip
  • j bzip2
  • J xz

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).

OptionArgumentMeaningExample
-c—Create archivetar -cf a.tar dir/
-x—Extract archivetar -xf a.tar
-t—List contentstar -tf a.tar
-fARCHIVEArchive file name (required for file ops)tar -tf a.tar
-v—Verbose; list files processedtar -cvf a.tar dir/
-z—gzip compress/decompresstar -czf a.tar.gz dir/
-j—bzip2tar -cjf a.tar.bz2 dir/
-J—xztar -cJf a.tar.xz dir/
-a—Auto-compress from suffix (GNU)tar -caf a.tar.gz dir/
-CDIRchdir before the next path (create or extract). Prefer this over archiving /absolute pathstar -czf a.tgz -C /etc myapp
-p—Restore modes exactly (non-root otherwise applies umask)tar -xpzf a.tgz
--same-owner—Preserve ownership if rootExtract as root
-h—Follow symlinks (store target)tar -chf a.tar link
--excludePATSkip patterntar -czf a.tgz --exclude='.git' dir
--exclude-fromFILEExclude list
-r—Append files to uncompressed tarRare
-u—Append only newerRare
--strip-componentsNStrip N leading path components on extracttar -xzf a.tgz --strip-components=1
-k—Don’t overwrite existing on extracttar -xkzf a.tgz
--overwrite—Overwrite (default often)
-W—Verify after write (GNU)
--numeric-owner—Use UIDs not namesPortable archives
-S—Handle sparse filesVM images
--checkpointNProgress every N recordsLarge archives
-I / --use-compress-programCMDCustom compressor (GNU). Newer tar also has --zstdtar -I zstd -cf a.tar.zst dir/

Flag combos

ComboMeaningExample
tar -czvf out.tar.gz dir/Create gzip tarball verboseStandard handoff
tar -xzvf out.tar.gzExtract gzip tarball
tar -tzvf out.tar.gzList gzip tarballInspect first
tar -xzvf out.tar.gz -C /opt/appExtract into directoryDeploys
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 /destFlatten top folderRelease tarballs

stdin/stdout:

  • tar -czf - dir/ > out.tgz
  • cat out.tgz | tar -xzf - -C /dest

gzip

Definition: Compress/decompress a single file with gzip; zcat reads without unpacking.

CommandArgumentMeaningExample
gzipFILECompress FILE → FILE.gz (removes original)gzip big.log
gzip -kFILEKeep original (GNU)gzip -k big.log
gzip -cFILEWrite compressed to stdoutgzip -c f > f.gz
gzip -dFILE.gzDecompress (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 9gzip -9 f
gzip -tFILE.gzTest integritygzip -t f.gz
gzip -v—Verbosegzip -v f
gunzipFILE.gzDecompressgunzip f.gz
gunzip -cFILE.gzDecompress to stdoutgunzip -c f.gz | less
zcatFILE.gzDecompress to stdoutzcat f.gz | grep ERR
zgrepPAT FILE.gzgrep inside gz (if installed)zgrep ERROR f.gz

Flag combos

ComboMeaningExample
gzip -k fileCompress keep source
zcat app.log.gz | tailPeek without full extract
vs targzip alone = one file; multi-file → use tar

bzip2

Definition: Alternative compressors (often via tar -j / tar -J). Includes xz.

CommandArgumentMeaningExample
bzip2FILECompress → .bz2 and delete the original (like gzip)bzip2 f
bunzip2FILE.bz2Decompressbunzip2 f.bz2
bzcatFILE.bz2stdoutbzcat f.bz2
xzFILECompress → .xz and delete the original (-k to keep)xz f
xz -d / unxzFILE.xzDecompressxz -d f.xz
xzcatFILE.xzstdoutxzcat f.xz
xz -tFILE.xzTestxz -t f.xz

With tar: -j = bzip2 · -J = xz.

zip

Definition: Create or extract ZIP archives (common for cross-platform handoff).

CommandArgumentMeaningExample
zipOUT.zip FILE...Create zipzip out.zip a.txt b.txt
zip -rOUT.zip DIRRecursive directoryzip -r out.zip dir/
zip -eOUT.zip ...Encrypt (prompt). Default is weak ZipCrypto, not AESCareful sharing
zip -uOUT.zip ...Update existing
zip -q—Quiet
unzipARCHIVE.zipExtract hereunzip out.zip
unzip -lARCHIVE.zipListunzip -l out.zip
unzip -dDIR ARCHIVEExtract into DIRunzip -d /opt out.zip
unzip -oARCHIVEOverwrite without promptScripts
unzip -nARCHIVENever overwriteSafe
unzip -tARCHIVETestunzip -t out.zip
unzip -vARCHIVEVerbose list
zipinfoARCHIVEDetailed listingIf installed

Flag combos

ComboMeaningExample
zip -r release.zip dist/Windows-friendly handoff
unzip -l release.zipInspect first
unzip -d /opt/app release.zipControlled extract path

7z

Definition: High-ratio archive tool for 7z and many other formats. Optional — install if missing.

OptionArgumentMeaningExample
aOUT SRCAdd to archive7z a out.7z dir/
xARCHIVEExtract with directory paths7z x out.7z
eARCHIVEExtract flat (drops folders)Easy to mix up with x
lARCHIVEList7z l out.7z
tARCHIVETest7z t out.7z

Common recipes

GoalCommand
Backup dir to tarballtar -czvf backup-$(date +%F).tar.gz -C /etc myapp
Extract into /optsudo tar -xzvf app.tar.gz -C /opt
List firsttar -tzvf app.tar.gz | head
Exclude junktar -czf src.tgz --exclude='.git' --exclude='*.o' src/
Compress single loggzip -k app.log
Read gz logzcat app.log.gz | less or zgrep ERROR app.log.gz
Zip projectzip -r project.zip project/ -x '*.git*'
Unpack zipunzip -d /tmp/out project.zip
Remote noteCombine with scp / rsync (sheet 16) after create

Pitfalls

  • In a flag cluster, put f last: tar -czvf out.tgz dir. tar -czfv out.tgz dir names the archive v. GNU tar accepts a separate -f out.tgz anywhere.
  • Archiving /etc/myapp stores that path in the tarball. Prefer tar -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 -t first; extract in an empty dir.
  • Extract as root restores owners (--same-owner is the default for root) — check ownership. Non-root extract applies umask unless -p.
  • gzip / bzip2 / xz FILE delete the original unless -k. Default gzip level is -6, not 9.
  • zip -e is ZipCrypto (weak). 7z x keeps paths; 7z e flattens 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.

Previous14 Package ManagementNext16 SSH & Remote Transfer