Skip to main content

Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.

7z - 7-Zip File Archiver

The 7z command is a high-performance file archiver that supports multiple compression formats with excellent compression ratios. It uses the LZMA2 compression algorithm (7z format) and can handle various archive formats including ZIP, RAR, TAR, GZIP, and more. 7z is known for its superior compression capabilities, especially for text files, and supports features like solid compression, encryption, and multi-volume archives.

Basic Syntax

7z [adeltux] [SWITCH] <ARCHIVE_NAME> [FILES...] [-x<EXCLUDE_FILES>]

Common Commands

  • a - Add files to archive
  • d - Delete files from archive
  • e - Extract files from archive (without using directory names)
  • l - List contents of archive
  • t - Test integrity of archive
  • u - Update files in archive
  • x - Extract files with full paths

Common Switches

Archive Options

  • -t{TYPE} - Set type of archive (7z, zip, gzip, bzip2, tar)
  • -m{METHOD} - Set compression method
  • -mx{LEVEL} - Set compression level (0-9)
  • -mmt{N} - Set number of CPU threads
  • -p{PASSWORD} - Set password
  • -mhe=on - Enable archive header encryption

File Selection

  • -i{FILE} - Include filenames
  • -x{FILE} - Exclude filenames
  • -r - Recurse subdirectories

Output Options

  • -y - Assume Yes on all queries
  • -o{DIR} - Set Output directory
  • -v{SIZE} - Create volumes

Information

  • -l - List contents
  • -slt - Show technical information
  • -p - Prompt for password

Usage Examples

Basic Archive Operations

Creating Archives

# Create 7z archive
7z a archive.7z file1.txt file2.txt

# Create archive with maximum compression
7z a -mx=9 archive.7z documents/

# Create archive recursively
7z a -r archive.7z /path/to/directory/

# Create archive with specific compression method
7z a -m0=lzma2 -mx=9 archive.7z files/

# Create archive with multiple threads
7z a -mmt=4 archive.7z large_directory/

# Create ZIP format archive
7z a -tzip archive.zip files/

# Create TAR.GZ archive
7z a -ttar -tgzip archive.tar.gz files/

Extracting Archives

# Extract with full paths
7z x archive.7z

# Extract to specific directory
7z x archive.7z -o /target/directory/

# Extract without paths (flatten)
7z e archive.7z

# Extract without prompting
7z x archive.7z -y

# Extract specific files
7z x archive.7z document.pdf

# Extract password-protected archive
7z x secure.7z -p"mypassword"

Advanced Compression

Compression Methods and Levels

# LZMA2 with maximum compression
7z a -m0=lzma2 -mx=9 archive.7z files/

# LZMA fast compression
7z a -m0=lzma -mx=5 archive.7z files/

# PPMd compression (best for text)
7z a -m0=ppmd -mx=9 archive.7z text_files/

# Copy mode (no compression)
7z a -m0=copy archive.7z already_compressed_files/

# Deflate64 method
7z a -m0=deflate64 -mx=9 archive.zip files/

# Create solid archive (better compression for many files)
7z a -ms=on archive.7z many_files/

Multi-threading and Performance

# Use all available CPU threads
7z a -mmt=on archive.7z large_directory/

# Use specific number of threads
7z a -mmt=8 archive.7z files/

# Create archive with large dictionary
7z a -m0=lzma2 -mx=9 -md=128m archive.7z files/

# Benchmark different settings
7z b -mmt=4 -md=64m

Password Protection and Encryption

Password Protection

# Create password-protected archive
7z a -p"mypassword" secure.7z sensitive_files/

# Create archive with encrypted filenames
7z a -p"mypass" -mhe=on ultra_secure.7z data/

# Extract password-protected archive
7z x secure.7z -p"mypassword"

# Extract with encrypted filenames
7z x ultra_secure.7z -p"mypass" -mhe=on

# Prompt for password (more secure)
7z a -p archive.7z secret_files/

Encryption Methods

# AES-256 encryption (default)
7z a -p"password" -mem=AES256 secure.7z files/

# Create encrypted archive
7z a -p"mypassword" -mhe=on -mem=AES256 encrypted.7z data/

Multi-volume Archives

Splitting Archives

# Create 100MB volumes
7z a -v100m large_archive.7z huge_directory/

# Create 1GB volumes
7z a -v1g backup.7z /backup/directory/

# Create 650MB volumes (for CDs)
7z a -v650m cd_archive.7z files/

# Create custom size volumes
7z a -v500000k archive.7z files/

# Extract multi-volume archive
7z x large_archive.7z.001

File Selection and Filtering

Include/Exclude Patterns

# Include specific file types
7z a archive.7z *.txt *.doc *.pdf

# Exclude files and directories
7z a archive.7z project/ -x"*.tmp" -x"*.log" -x"node_modules/*"

# Include only files from list file
7z a archive.7z -i@filelist.txt

# Exclude files from list
7z a archive.7z -x@exclude.txt

# Complex include/exclude patterns
7z a -r archive.7z /data/ -x"*.cache" -x"temp/*" -x"*.bak"

Advanced Filtering

# Archive based on file size
find /data -size +10M -exec 7z a large_files.7z {} +

# Archive based on date
find /data -mtime -7 -exec 7z a recent.7z {} +

# Archive specific permissions
find /data -perm 644 -exec 7z a public.7z {} +

Practical Examples

System Administration

System Backup

# Create compressed system backup
7z a -m0=lzma2 -mx=9 -mhe=on -p"backup_pass" system_backup.7z /etc /home /var

# Create incremental backup
7z u -m0=lzma2 -mx=9 backup.7z /home/user/documents

# Create encrypted backup with volumes
7z a -v1g -mhe=on -p"secure_pass" backup_vol.7z /important/data/

# Test backup integrity
7z t backup.7z

Log Management

# Compress old log files
7z a -m0=lzma2 -mx=5 logs_archive.7z /var/log/*.log.*

# Create compressed archive with date
7z a logs_$(date +%Y%m%d).7z /var/log/ -x"*.log" -x"*.tmp"

# Extract specific logs for analysis
7z x logs_archive.7z -o /tmp/ "*error*"

# Create encrypted log archive
7z a -p"logpass" secure_logs.7z /var/log/audit/

Development Workflow

Software Distribution

# Create compressed source distribution
7z a -mx=9 project-1.0.0.7z src/ -x"*.git*" -x"node_modules/*" -x"*.log"

# Create portable application package
7z a -m0=lzma2 -mmt=on portable_app.7z app/

# Create development tools archive
7z a dev_tools.7z /usr/local/bin/tool1 /usr/local/bin/tool2

# Create documentation archive
7z a docs.7z docs/ README.md LICENSE CHANGELOG.md

Data Processing

# Archive large datasets efficiently
7z a -m0=lzma2 -mx=9 -mmt=on dataset.7z large_dataset/

# Archive with progress monitoring
7z a -m0=lzma2 -mx=9 big_data.7z /data/ -p"pass" -y

# Create archive for cloud storage
7z a -v100m cloud_backup.7z /upload/directory/

# Archive and encrypt research data
7z a -mhe=on -p"research_pass" -mx=9 research_data.7z /data/experiments/

Advanced Usage

Archive Management

Updating Archives

# Add files to existing archive
7z a existing.7z new_file.txt

# Update files in archive
7z u archive.7z /path/to/updated/files/

# Delete files from archive
7z d archive.7z unwanted_file.txt

# Rename files in archive (extract and recreate)
7z x archive.7z -o temp/
7z a new_archive.7z temp/*

Testing and Verification

# Test archive integrity
7z t archive.7z

# Test with detailed output
7z t archive.7z -p"password"

# List archive contents with details
7z l -slt archive.7z

# Check compression statistics
7z l archive.7z | tail -1

# Test password-protected archive
7z t secure.7z -p"password"

Performance Optimization

Multi-threading

# Use all available cores
7z a -mmt=on archive.7z large_files/

# Benchmark compression
7z b -m0=lzma2 -mx=9 -mmt=8

# Test different thread counts
for threads in {1,2,4,8}; do
echo "Testing $threads threads:"
time 7z a -mmt=$threads test.7z large_file
done

Memory and Dictionary Settings

# Use large dictionary for better compression
7z a -md=256m archive.7z files/

# Use specific memory settings
7z a -md=64m -mmt=4 archive.7z files/

# Benchmark different dictionary sizes
7z b -md=32m
7z b -md=64m
7z b -md=128m

Special Operations

Working with Different Formats

Format Conversions

# Convert TAR to 7Z
7z a archive.7z -t7z archive.tar

# Create ZIP archive with 7-Zip
7z a archive.zip -tzip files/

# Extract various formats
7z x archive.rar
7z x archive.tar.gz
7z x archive.bz2

# Create self-extracting archive (Windows)
7z a -sfx archive.sfx files/

Cross-platform Archives

# Create Windows-compatible ZIP
7z a -tzip -mem=Deflate archive.zip files/

# Create archive with specific file attributes
7z a -tzip archive.zip files/ -x"*.sh"

# Create POSIX-compatible archive
7z a -ttar archive.tar files/

Integration and Automation

Shell Scripts

Automated Backup Script

#!/bin/bash
# Advanced backup with 7-Zip

BACKUP_DIR="/backups"
SOURCE_DIR="/home/user/documents"
DATE=$(date +%Y%m%d_%H%M%S)
PASSWORD="your_backup_password"

# Create encrypted backup with volumes
7z a -v1g -mhe=on -p"$PASSWORD" -mx=9 \
"$BACKUP_DIR/backup_$DATE.7z" \
"$SOURCE_DIR" \
-x"*.tmp" -x"*.cache" -x"node_modules/*"

# Test backup integrity
if 7z t "$BACKUP_DIR/backup_$DATE.7z" -p"$PASSWORD"; then
echo "Backup created successfully: backup_$DATE.7z"
else
echo "Backup verification failed!"
exit 1
fi

File Archiving Script

#!/bin/bash
# Archive old files based on criteria

ARCHIVE_DIR="/archive"
SOURCE_DIR="/data"
DAYS_OLD=30

# Create archive of files older than N days
find "$SOURCE_DIR" -type f -mtime +$DAYS_OLD -print0 | \
xargs -0 7z a -mx=9 -r "$ARCHIVE_DIR/old_files_$(date +%Y%m%d).7z"

# Remove original files after successful archiving
if [ $? -eq 0 ]; then
find "$SOURCE_DIR" -type f -mtime +$DAYS_OLD -delete
echo "Files archived and removed successfully"
else
echo "Archive creation failed, files not removed"
fi

Troubleshooting

Common Issues

Memory Issues

# Out of memory errors
# Solution: Reduce dictionary size or threads
7z a -md=32m -mmt=2 archive.7z files/

# Monitor memory usage
/usr/bin/time -v 7z a archive.7z files/

# Use less memory-intensive settings
7z a -mx=5 -md=16m archive.7z files/

Performance Issues

# Slow compression
# Solution: Adjust threads or compression level
7z a -mmt=on -mx=5 archive.7z files/

# Benchmark different settings
7z b -m0=lzma2 -mx=5
7z b -m0=lzma2 -mx=7
7z b -m0=lzma2 -mx=9

Archive Corruption

# Test archive integrity
7z t archive.7z

# Test with password
7z t secure.7z -p"password"

# Attempt to extract with recovery
7z x -r corrupted.7z

# Check specific files
7z t archive.7z specific_file.txt
  • p7zip - 7-Zip for POSIX systems
  • zip - ZIP format archiver
  • unzip - Extract ZIP archives
  • tar - Tape archiver
  • gzip - GZIP compression
  • bzip2 - BZIP2 compression
  • xz - XZ compression
  • rar - RAR archiver

Best Practices

  1. Use LZMA2 with high compression (mx=9) for archival purposes
  2. Enable multi-threading (-mmt=on) on multi-core systems
  3. Use solid archives (-ms=on) for many similar files
  4. Test archives after creation with 7z t
  5. Use encryption (-mhe=on) for sensitive data
  6. Choose appropriate compression level based on speed vs. size needs
  7. Use dictionary size (-md) based on available memory
  8. Create volumes (-v) for large files or network transfers
  9. Use include/exclude patterns for selective archiving
  10. Monitor system resources during large operations

Performance Tips

  1. LZMA2 provides best compression for most file types
  2. PPMd is excellent for text files
  3. Multi-threading significantly speeds up compression
  4. Solid archives improve compression for many small files
  5. Higher dictionary sizes improve compression but use more memory
  6. Compression level 9 provides best compression but is slowest
  7. Level 5-6 provides good balance for most use cases
  8. Copy mode (level 0) for already compressed files

The 7z command is a powerful archiver offering superior compression ratios and extensive format support. Its advanced features like solid compression, strong encryption, and multi-threading make it ideal for creating highly compressed archives for storage, distribution, and backup purposes.