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 listing

Common 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

bzip2

xz

📊 Compression Comparison

Tool
Speed
Compression Ratio
File Extension

gzip

Fast

Good

.gz

bzip2

Slower

Better

.bz2

xz

Slowest

Best

.xz

🔄 Working with zip Files

💡 Best Practices

  1. Choose the Right Tool

    • Use tar for preserving permissions and directory structure

    • Use gzip for quick compression

    • Use xz for best compression ratio

    • Use zip for cross-platform compatibility

  2. Backup Before Extraction

    • Always verify archive contents before extracting

    • Use -t option with tar to test archive integrity

    • Extract to temporary directory first

  3. Preserve Metadata

    • Use -p with tar to preserve permissions

    • Use -a to preserve access times

    • Consider using --xattrs for extended attributes

🚀 Advanced Usage

Creating Encrypted Archives

Excluding Files

Incremental Backup

Last updated