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
| Format | Command | Speed | Compression |
|---|
| gzip | -czf | Fast | Good |
| bzip2 | -cjf | Slow | Better |
| xz | -cJf | Slowest | Best |
| zstd | --zstd | Very fast | Very 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 ...