Creating Archives
# Create a tar archive
tar -cf archive.tar /path/to/files
# Create compressed with gzip
tar -czf archive.tar.gz /path/to/files
# Create compressed with bzip2 (smaller, slower)
tar -cjf archive.tar.bz2 /path/to/files
# Create compressed with xz (smallest, slowest)
tar -cJf archive.tar.xz /path/to/files
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
Viewing Contents
# List files without extracting
tar -tf archive.tar.gz
# Verbose listing
tar -tvf archive.tar.gz
Advanced Usage
# Exclude patterns
tar -czf backup.tar.gz /var/www --exclude="*.log" --exclude="node_modules"
# Extract single file
tar -xzf archive.tar.gz path/to/specific/file
# Append to existing archive
tar -rf archive.tar newfile.txt
Other Compression Tools
# zip/unzip (for compatibility)
zip -r archive.zip /path/to/files
unzip archive.zip
# Individual file compression
gzip file.txt # Creates file.txt.gz
gunzip file.txt.gz # Restores file.txt