Archive
This guide covers common file archiving and compression techniques in Linux systems.
π¦ tar (Tape Archive)
Basic tar Operations
# Create tar archive
tar -cf archive.tar files/ # Create archive
tar -czf archive.tar.gz files/ # Create compressed archive (gzip)
tar -cjf archive.tar.bz2 files/ # Create compressed archive (bzip2)
tar -cJf archive.tar.xz files/ # Create compressed archive (xz)
# Extract tar archive
tar -xf archive.tar # Extract archive
tar -xzf archive.tar.gz # Extract gzip archive
tar -xjf archive.tar.bz2 # Extract bzip2 archive
tar -xJf archive.tar.xz # Extract xz archive
# List contents
tar -tf archive.tar # List contents without extracting
tar -tvf archive.tar # Verbose listingCommon tar Options
-c: Create archive-x: Extract archive-f: Specify filename-v: Verbose output-z: Use gzip compression-j: Use bzip2 compression-J: Use xz compression-t: List contents-p: Preserve permissions
ποΈ Compression Tools
gzip
# Compress file
gzip file.txt # Creates file.txt.gz
gzip -k file.txt # Keep original file
# Decompress file
gunzip file.txt.gz
gzip -d file.txt.gzbzip2
# Compress file
bzip2 file.txt # Creates file.txt.bz2
bzip2 -k file.txt # Keep original file
# Decompress file
bunzip2 file.txt.bz2
bzip2 -d file.txt.bz2xz
# Compress file
xz file.txt # Creates file.txt.xz
xz -k file.txt # Keep original file
# Decompress file
unxz file.txt.xz
xz -d file.txt.xzπ Compression Comparison
gzip
Fast
Good
.gz
bzip2
Slower
Better
.bz2
xz
Slowest
Best
.xz
π Working with zip Files
# Create zip archive
zip archive.zip files/ # Create archive
zip -r archive.zip directory/ # Include subdirectories
# Extract zip archive
unzip archive.zip # Extract archive
unzip -l archive.zip # List contentsπ‘ Best Practices
Choose the Right Tool
Use
tarfor preserving permissions and directory structureUse
gzipfor quick compressionUse
xzfor best compression ratioUse
zipfor cross-platform compatibility
Backup Before Extraction
Always verify archive contents before extracting
Use
-toption with tar to test archive integrityExtract to temporary directory first
Preserve Metadata
Use
-pwith tar to preserve permissionsUse
-ato preserve access timesConsider using
--xattrsfor extended attributes
π Advanced Usage
Creating Encrypted Archives
# Using zip with encryption
zip -e secure.zip files/
# Using tar with gpg
tar -czf - files/ | gpg -c > secure.tar.gz.gpgExcluding Files
# Exclude specific files/patterns
tar -czf archive.tar.gz --exclude='*.log' --exclude='temp/' directory/Incremental Backup
# Create snapshot file
tar -czf backup.tar.gz -g snapshot.file directory/
# Create incremental backup
tar -czf backup-inc.tar.gz -g snapshot.file directory/Last updated
Was this helpful?