tar Cheatsheet - tar Archive & Compression Reference
Before backing up a directory, shipping code, or migrating a server, you will pack it with tar. This table makes the create-versus-extract commands for each compression format (-z/-j/-J) unmistakable and covers advanced but frequently-asked cases like excluding files, guarding against accidental directory deletes, and incremental backups.
Pack & Compress 7
tar -cvf archive.tar dir/Package into .tar (no compression): -c create, -v verbose, -f file
tar -czvf archive.tar.gz dir/Package and gzip-compress (.tar.gz/.tgz), most common
tar -cjvf archive.tar.bz2 dir/Package and bzip2-compress; higher ratio, slower than gzip
tar -cJvf archive.tar.xz dir/Package and xz-compress; highest ratio, slowest
tar --zstd -cvf archive.tar.zst dir/zstd compression; much faster than gzip, recommended on newer systems
tar -czvf archive.tar.gz --exclude="*.log" dir/Exclude .log files while packaging
tar -czvf archive.tar.gz --exclude="node_modules" --exclude=".git" project/Exclude multiple directories
Extract 8
tar -xvf archive.tarExtract a .tar: -x extract
tar -xzvf archive.tar.gzExtract .tar.gz/.tgz
tar -xjvf archive.tar.bz2Extract .tar.bz2/.tbz2
tar -xJvf archive.tar.xzExtract .tar.xz/.txz
tar --zstd -xvf archive.tar.zstExtract .tar.zst
tar -xzvf archive.tar.gz -C /opt/Extract to a target directory (-C)
tar -xzvf archive.tar.gz file.txtExtract only a specified file from the archive
tar -xzvf archive.tar.gz --strip-components=1Drop the top-level directory on extract
List & Test 5
tar -tf archive.tar.gzList archive contents without extracting: -t list
tar -tzvf archive.tar.gzList with file size and permissions
tar -tf archive.tar.gz | grep "config"Find a specific file in the archive
gzip -t archive.tar.gzTest gzip integrity; no output means OK
tar -dvf archive.tarCompare archived files against those on disk
Compression Format Comparison 6
-z (gzip)Fastest, best compatibility, .tar.gz/.tgz
-j (bzip2)Higher ratio than gzip, slower, .tar.bz2
-J (xz)Highest ratio, slowest, .tar.xz
--zstd (zstd)Fast and good ratio, recommended on newer systems, .tar.zst
-aAuto-select compression by file extension
--use-compress-program=pigzUse pigz for multi-threaded gzip acceleration
Other Compression Tools 8
zip -r archive.zip dir/Zip a directory
unzip archive.zip -d /opt/Unzip to a target directory
unzip -l archive.zipList zip contents without extracting
gzip file.txtCompress a single file to file.txt.gz (original removed)
gzip -k file.txtCompress while keeping the original (-k keep)
gunzip file.txt.gzDecompress a single .gz file
zcat file.txt.gzView .gz contents without decompressing
7z x archive.7zExtract 7z format (requires p7zip)
Advanced Usage 6
tar -rvf archive.tar newfile.txtAppend a file to an existing .tar (not compressed)
tar -uvf archive.tar dir/Update only files newer than those in the archive
tar -czf - dir/ | ssh host "tar -xzf - -C /dest"Transfer and extract over an SSH pipe
tar -czf - dir/ | split -b 100M - archive.tar.gz.Split into 100 MB volumes
cat archive.tar.gz.* | tar -xzvf -Concatenate volumes and extract
tar -g snapshot.snar -czf backup.tar.gz dir/Incremental backup with a snapshot file
Common Examples 4
tar -czvf backup-$(date +%F).tar.gz /home/user/Dated directory backup
tar -czvf app.tar.gz --exclude="*.log" --exclude="tmp/" app/Package a project excluding logs and temp files
tar -xzvf package.tar.gz --strip-components=1 -C /opt/appExtract to a dir and drop the top level
tar -czf - /data | pigz > backup.tar.gzMulti-threaded compress of a large dir
Tips
- Mnemonic: c=create, x=extract, t=list, z=gzip, j=bzip2, J=xz.
- Before extracting, run tar -tf to inspect contents and confirm there's no ../ path traversal (zip-slip risk).
- For large files, zstd --long=31 uses long-distance matching — 5-10x faster than gzip with better ratio.
- Appending works only on uncompressed .tar; for .tar.gz you must decompress and repackage.
- pigz is the multi-threaded gzip; for large files use tar -cf - dir | pigz > out.tar.gz to speed up greatly.
- Put -C (target dir) after the filename; --strip-components drops the first N directory levels to avoid nested extraction.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us