tar command in Linux: tar -czf archive.tar.gz dir/ to create compressed archive, tar -xzf archive.tar.gz to extract, tar -tzf archive.tar.gz to list. Flags per man tar: -c create, -x extract, -t list, -z gzip, -j bzip2, -J xz, -f file, -v verbose, -C directory.
Table of contents
- Create an archive
- Extract an archive
- List contents
- Add files to existing archive
- Exclude patterns
- Streaming (no temp file)
- Verify archive
- FAQ
Create an archive
# .tar (uncompressed)
tar -cf archive.tar /path/to/dir/
# .tar.gz (gzip compressed)
tar -czf archive.tar.gz /path/to/dir/
# .tar.bz2 (bzip2, better compression)
tar -cjf archive.tar.bz2 /path/to/dir/
# .tar.xz (xz, best compression)
tar -cJf archive.tar.xz /path/to/dir/
The team that uses tar -czf has the standard archive format.
Extract an archive
# Auto-detect format
tar -xf archive.tar.gz
# Specific format
tar -xzf archive.tar.gz
tar -xjf archive.tar.bz2
tar -xJf archive.tar.xz
# To specific directory
tar -xzf archive.tar.gz -C /target/dir/
The team that uses -C /target/ has clean extraction to a specific path.
List contents
tar -tzf archive.tar.gz
The team that lists before extracting has visibility.
Add files to existing archive
tar -rf archive.tar new_file.txt
The team that updates existing archives has incremental backups.
Exclude patterns
tar -czf archive.tar.gz --exclude='*.log' --exclude='.git' /path/to/dir/
The team that excludes noise files (.log, .git, node_modules) has clean archives.
Streaming (no temp file)
# Archive to remote host via ssh
tar -czf - /local/dir/ | ssh user@remote 'cat > /backup/remote.tar.gz'
# Extract from remote
ssh user@remote 'cat /backup/remote.tar.gz' | tar -xzf -
The team that uses streaming has efficient remote backups without intermediate files.
Verify archive
tar -tzf archive.tar.gz > /dev/null && echo "OK" || echo "corrupt"
# Or with W flag (verify writes)
tar -xzvWf archive.tar.gz
The team that verifies archive integrity has confidence.
FAQ
What’s the difference between tar and zip?
tar: Unix-native, often combined with gzip. zip: Windows-friendly, single tool. The team that picks based on portability has the right choice.
Which compression is best?
gzip: fast, decent ratio. bzip2: slower, better ratio. xz: slowest, best ratio. The team that picks based on speed/size tradeoff.
Can I add files to an existing .tar.gz?
Yes with -r flag (rebuilds the archive). The team that does incremental backups uses this.
How do I extract a single file from a tar?
tar -xzf archive.tar.gz path/to/file.txt extracts just that file.
Why is tar so slow on small files?
Per-file overhead. The team that has many small files uses tar -czf - piped to a remote location.
If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.
Useful related references: