Docs / Linux Basics / Working with tar and gzip: File Compression on Linux

Working with tar and gzip: File Compression on Linux

By Admin · Feb 25, 2026 · Updated Apr 23, 2026 · 40 views · 2 min read

Creating Archives

# Create a tar archive
tar -cf archive.tar /path/to/directory

# Create a gzip-compressed archive
tar -czf archive.tar.gz /path/to/directory

# Create with bzip2 compression (smaller, slower)
tar -cjf archive.tar.bz2 /path/to/directory

# Create with zstd compression (fast, good ratio)
tar --zstd -cf archive.tar.zst /path/to/directory

Extracting Archives

# Extract tar
tar -xf archive.tar

# Extract gzip
tar -xzf archive.tar.gz

# Extract to specific directory
tar -xzf archive.tar.gz -C /destination/

# Extract bzip2
tar -xjf archive.tar.bz2

Listing Contents

# List files without extracting
tar -tzf archive.tar.gz

# Verbose listing (permissions, sizes)
tar -tvzf archive.tar.gz

Selective Operations

# Archive specific files
tar -czf backup.tar.gz file1.txt file2.txt dir1/

# Exclude patterns
tar -czf backup.tar.gz --exclude="*.log" --exclude="node_modules" /var/www

# Extract a single file
tar -xzf archive.tar.gz path/to/specific/file.txt

Compression Comparison

FormatCommandSpeedCompression
gzip-czfFastGood
bzip2-cjfSlowBetter
xz-cJfSlowestBest
zstd--zstdVery fastVery good

Individual File Compression

# Compress a single file
gzip large_file.log        # Creates large_file.log.gz (removes original)
gzip -k large_file.log     # Keep original

# Decompress
gunzip large_file.log.gz
gzip -d large_file.log.gz

Practical Tips

  • Use tar -czf (gzip) for most backup scenarios — good balance of speed and compression
  • Use zstd when available — faster than gzip with better compression
  • Always use the -v flag when debugging: tar -czvf archive.tar.gz ...

Was this article helpful?