Last updated
TAR Command Generator Examples
The TAR Command Generator builds correct tar commands for creating, extracting, listing, and managing archives on Unix and Linux systems. Below are practical examples for the most common archiving tasks.
Create a Compressed Archive
# Create a .tar.gz archive of a directory
tar -czf archive.tar.gz /path/to/directory
# Create a .tar.gz archive of multiple files and directories
tar -czf backup.tar.gz file1.txt file2.txt /path/to/dir
# Create a .tar.bz2 archive (better compression, slower)
tar -cjf archive.tar.bz2 /path/to/directory
# Create a .tar.xz archive (best compression ratio)
tar -cJf archive.tar.xz /path/to/directory
# Create a .tar.zst archive (fast + good compression, modern systems)
tar -c --zstd -f archive.tar.zst /path/to/directory
Flag breakdown: -c = create, -z = gzip, -j = bzip2, -J = xz, -f = filename follows.
Extract an Archive
# Extract a .tar.gz to the current directory
tar -xzf archive.tar.gz
# Extract to a specific directory
tar -xzf archive.tar.gz -C /path/to/destination
# Extract a .tar.bz2 archive
tar -xjf archive.tar.bz2 -C /opt/app
# Extract a .tar.xz archive
tar -xJf archive.tar.xz -C /usr/local
# Extract with verbose output (shows each file)
tar -xzvf archive.tar.gz -C /tmp/extracted
List Archive Contents Without Extracting
# List contents of a .tar.gz
tar -tzf archive.tar.gz
# List contents of a .tar.bz2
tar -tjf archive.tar.bz2
# List with verbose details (permissions, size, date)
tar -tvzf archive.tar.gz
# List contents of any tar archive (auto-detect compression)
tar -tf archive.tar.gz
Extract Specific Files from an Archive
# Extract a single file
tar -xzf archive.tar.gz path/inside/archive/file.txt
# Extract multiple specific files
tar -xzf archive.tar.gz file1.txt config/settings.json
# Extract files matching a pattern
tar -xzf archive.tar.gz --wildcards '*.json'
# Extract files matching a pattern (GNU tar)
tar -xzf archive.tar.gz --wildcards --no-anchored '*.log'
Exclude Files and Directories
# Exclude a specific directory
tar -czf backup.tar.gz /project --exclude=/project/node_modules
# Exclude multiple patterns
tar -czf backup.tar.gz /project \
--exclude='node_modules' \
--exclude='.git' \
--exclude='*.log' \
--exclude='dist'
# Exclude using an exclusion file
tar -czf backup.tar.gz /project --exclude-from=.tarignore
The .tarignore file uses the same pattern format as .gitignore.
Strip Leading Path Components on Extract
# Archive has: myproject-1.0/src/main.js
# Extract without the top-level directory
tar -xzf myproject-1.0.tar.gz --strip-components=1
# Result: src/main.js (top-level directory removed)
# Strip 2 levels
tar -xzf archive.tar.gz --strip-components=2
This is useful when downloading tarballs from GitHub or other sources that include a version-named top-level directory.
Preserve Permissions and Ownership
# Create archive preserving permissions (default behavior)
tar -czf backup.tar.gz /etc/nginx
# Extract preserving permissions (requires root for ownership)
sudo tar -xzf backup.tar.gz -C /
# Extract preserving permissions as current user
tar -xzpf backup.tar.gz -C /restore
Incremental Backup
# Create a full backup with snapshot file
tar -czf full-backup.tar.gz -g snapshot.snar /data
# Create an incremental backup (only changed files since last snapshot)
tar -czf incremental-backup.tar.gz -g snapshot.snar /data
# Restore: first restore full, then each incremental in order
tar -xzf full-backup.tar.gz -C /restore
tar -xzf incremental-backup.tar.gz -C /restore
Create Archive of Files Newer Than a Date
# Archive files modified in the last 24 hours
tar -czf recent.tar.gz --newer-mtime="1 day ago" /project/src
# Archive files modified after a specific date
tar -czf changes.tar.gz --newer-mtime="2024-01-01" /project
# Archive files newer than a reference file
tar -czf updates.tar.gz --newer=reference.txt /project/src
Packaging a Deployment Artifact
# Package a Node.js app for deployment (exclude dev files)
tar -czf deploy.tar.gz \
--exclude='node_modules' \
--exclude='.git' \
--exclude='*.test.js' \
--exclude='.env*' \
-C /project .
# Package a compiled Go binary with config
tar -czf release-v1.0.tar.gz \
bin/myapp \
config/production.yaml \
README.md
Split Large Archives
# Create a split archive (1GB parts)
tar -czf - /large/directory | split -b 1G - backup.tar.gz.part
# Reassemble and extract
cat backup.tar.gz.part* | tar -xzf -
Common Flag Reference
- -c : create a new archive
- -x : extract files from archive
- -t : list archive contents
- -f : specify archive filename (must be followed by filename)
- -z : compress/decompress with gzip (.tar.gz)
- -j : compress/decompress with bzip2 (.tar.bz2)
- -J : compress/decompress with xz (.tar.xz)
- -v : verbose — list files as they are processed
- -p : preserve file permissions
- -C : change to directory before operating
- --exclude : exclude files matching pattern
- --strip-components=N : remove N leading path components on extract
- -g : use snapshot file for incremental backups
- --newer-mtime : include only files newer than date
Use the TAR Command Generator to select your operation, compression format, and options, and get the exact command ready to copy and run.