tar Cheat Sheet
Create, inspect, and extract archives with compression — the universal packaging tool.
Core DevOps4 sections · 21 commands
⌘K
Creating Archives
6tar -cvf ARCHIVE.tar DIRCreate a plain archive (verbose)
tar -czf ARCHIVE.tar.gz DIRCreate with gzip compression
tar -cjf ARCHIVE.tar.bz2 DIRCreate with bzip2
tar -cJf ARCHIVE.tar.xz DIRCreate with xz (best ratio)
tar -czf ARCHIVE.tar.gz --exclude='*.log' DIRExclude files by pattern
tar -czf ARCHIVE.tar.gz DIR --exclude-backupsSkip backup files
Extracting
7tar -xzf ARCHIVE.tar.gzExtract a gzipped archive
tar -xf ARCHIVE.tar -C DIRExtract into a specific directory
tar -xzf ARCHIVE.tar.gz --strip-components=1Drop the top-level folder
tar -xJf ARCHIVE.tar.xzExtract xz (auto-detect also works)
tar -xzf ARCHIVE.tar.gz FILEExtract a single file
tar -xzf ARCHIVE.tar.gz 'pattern*'Extract files matching a pattern
tar -xzf ARCHIVE.tar.gz --one-top-level=DIRExtract under a prefix directory
Inspecting
4tar -tzf ARCHIVE.tar.gzList contents without extracting
tar -tvzf ARCHIVE.tar.gzList with sizes and permissions
tar -xzf ARCHIVE.tar.gz --occurrence=1 FILEExtract first match only
gzip -t FILE.tar.gzVerify the archive's integrity
Streaming & Incremental
4tar -czf - DIR | scp - USER@HOST:Archive over SSH without a temp file
tar --listed-incremental=SNAPSHOT -czf BACKUP.tar.gz DIRIncremental with a snapshot file
tar -xzf ARCHIVE.tar.gz -C DIR --same-owner --preserve-permissionsRestore ownership and modes
tar -czPf ARCHIVE.tar.gz /abs/pathPreserve absolute paths (P)