Tar Cheatsheet

Complete tar command reference for archiving and compression. Search, filter by category, and click to copy.

Tar Cheatsheet

Archive and compression commands for Linux/Unix

76

Commands

7

Categories

0

Favorites

18

Sections

Create
Basic Archive Creation
tar -cvf archive.tar /path/to/dir

Create archive from directory

tar -cvf archive.tar file1 file2 file3

Create archive from multiple files

tar -cvf archive.tar *.txt

Archive all .txt files

tar -cvf archive.tar -C /path dir

Archive with changed directory

tar -cvf - /path | ssh user@host 'cat > archive.tar'

Create and send to remote

Create
Compressed Archives
tar -czvf archive.tar.gz /path/to/dir

Create gzip compressed archive

tar -cjvf archive.tar.bz2 /path/to/dir

Create bzip2 compressed archive

tar -cJvf archive.tar.xz /path/to/dir

Create xz compressed archive

tar -cavf archive.tar.gz /path/to/dir

Auto-detect compression by extension

💡 -z: gzip, -j: bzip2, -J: xz, -a: auto-detect

Create
Exclude Files
tar -cvf archive.tar --exclude='*.log' /path

Exclude .log files

tar -cvf archive.tar --exclude='node_modules' /path

Exclude directory

tar -cvf archive.tar --exclude-from=exclude.txt /path

Exclude from file list

tar -cvf archive.tar --exclude-vcs /path

Exclude version control dirs

Extract
Basic Extraction
tar -xvf archive.tar

Extract tar archive

tar -xvf archive.tar -C /destination

Extract to specific directory

tar -xvf archive.tar file.txt

Extract single file

tar -xvf archive.tar --wildcards '*.txt'

Extract files matching pattern

tar -xvf archive.tar --strip-components=1

Remove leading directory

Extract
Extract Compressed
tar -xzvf archive.tar.gz

Extract gzip archive

tar -xjvf archive.tar.bz2

Extract bzip2 archive

tar -xJvf archive.tar.xz

Extract xz archive

tar -xavf archive.tar.gz

Auto-detect and extract

Extract
Extract Options
tar -xvf archive.tar --keep-old-files

Don't overwrite existing files

tar -xvf archive.tar --overwrite

Overwrite existing files

tar -xvf archive.tar --no-same-owner

Don't preserve ownership

tar -xvf archive.tar --no-same-permissions

Don't preserve permissions

List
View Contents
tar -tvf archive.tar

List contents with details

tar -tf archive.tar

List contents (names only)

tar -tzvf archive.tar.gz

List gzip archive contents

tar -tjvf archive.tar.bz2

List bzip2 archive contents

tar -tJvf archive.tar.xz

List xz archive contents

List
Search & Filter
tar -tvf archive.tar | grep 'pattern'

Search for files in archive

tar -tvf archive.tar --wildcards '*.txt'

List files matching pattern

tar -tvf archive.tar | head -20

Show first 20 entries

tar -tvf archive.tar | wc -l

Count files in archive

Modify
Add & Update
tar -rvf archive.tar newfile.txt

Append file to archive

tar -rvf archive.tar newdir/

Append directory to archive

tar -uvf archive.tar file.txt

Update file if newer

tar -Avf archive.tar other.tar

Append another archive

💡 Cannot add to compressed archives directly

Modify
Delete & Compare
tar --delete -f archive.tar file.txt

Delete file from archive

tar --delete -f archive.tar 'dir/*'

Delete directory contents

tar -dvf archive.tar

Compare archive with filesystem

tar --diff -f archive.tar

Find differences

Compression
Compression Levels
tar -cvf - dir | gzip -9 > archive.tar.gz

Maximum gzip compression

tar -cvf - dir | bzip2 -9 > archive.tar.bz2

Maximum bzip2 compression

tar -cvf - dir | xz -9 > archive.tar.xz

Maximum xz compression

GZIP=-9 tar -czvf archive.tar.gz dir

Set gzip level via env

Compression
Other Compression
tar -cvf - dir | lz4 > archive.tar.lz4

LZ4 compression (fast)

tar -cvf - dir | zstd > archive.tar.zst

Zstandard compression

tar -cvf - dir | pigz > archive.tar.gz

Parallel gzip (faster)

tar -cvf - dir | pbzip2 > archive.tar.bz2

Parallel bzip2

💡 Parallel tools use multiple CPU cores

Backup
Incremental Backup
tar -cvf backup.tar -g snapshot.snar /data

Create incremental backup

tar -cvf backup-1.tar -g snapshot.snar /data

First incremental

tar -cvf backup-2.tar -g snapshot.snar /data

Next incremental

tar -xvf backup.tar -g /dev/null

Restore incremental

💡 Snapshot file tracks changes between backups

Backup
Backup Options
tar -cvpf backup.tar /data

Preserve permissions

tar -cvf backup.tar --newer='2024-01-01' /data

Files newer than date

tar -cvf backup.tar --after-date='2024-01-01' /data

Modified after date

tar -cvf - /data | split -b 1G - backup.tar.

Split into 1GB parts

Advanced
Remote Operations
tar -czvf - /data | ssh user@host 'cat > backup.tar.gz'

Archive to remote server

ssh user@host 'tar -czvf - /data' > backup.tar.gz

Archive from remote server

tar -czvf - /data | ssh user@host 'tar -xzvf - -C /dest'

Transfer and extract

rsync -avz --delete /data/ user@host:/backup/

Alternative: rsync

Advanced
Special Options
tar -cvf archive.tar --sparse /path

Handle sparse files efficiently

tar -cvf archive.tar --totals /path

Show total bytes written

tar -cvf archive.tar --checkpoint=1000 /path

Show progress every 1000 records

tar -cvf archive.tar --transform 's/old/new/' /path

Transform filenames

tar -cvf archive.tar --owner=root --group=root /path

Set owner/group

Advanced
Verification
tar -tvf archive.tar > /dev/null && echo 'OK'

Verify archive integrity

gzip -t archive.tar.gz && echo 'OK'

Test gzip integrity

bzip2 -t archive.tar.bz2 && echo 'OK'

Test bzip2 integrity

xz -t archive.tar.xz && echo 'OK'

Test xz integrity

Advanced
Useful Combinations
tar -czvf backup-$(date +%Y%m%d).tar.gz /data

Dated backup filename

find /data -mtime -1 | tar -cvf backup.tar -T -

Archive recently modified

tar -czvf - /data | pv | ssh user@host 'cat > backup.tar.gz'

With progress bar

cat backup.tar.* | tar -xvf -

Extract split archive

Quick Reference - Common Flags

-c

Create

-x

Extract

-t

List

-v

Verbose

-f

File

-z

Gzip

-j

Bzip2

-J

XZ

About Tar Command

The tar (tape archive) command is a powerful utility for creating, extracting, and managing archive files in Unix/Linux systems. It can combine multiple files into a single archive and supports various compression formats including gzip (.tar.gz), bzip2 (.tar.bz2), and xz (.tar.xz).

Common File Extensions

  • .tar - Uncompressed archive
  • .tar.gz or .tgz - Gzip compressed
  • .tar.bz2 or .tbz2 - Bzip2 compressed
  • .tar.xz or .txz - XZ compressed