7z (via 7-Zip’s Linux port) lets you create, view, and extract archives in multiple formats—including its own .7z, ZIP, TAR, GZIP, BZIP2, and more—directly from the terminal. It’s highly efficient for compressing large data sets and supports strong AES-256 encryption. To use it, you need the p7zip package installed (the standard Linux implementation of 7-Zip). The steps below cover installation, basic operations, and troubleshooting.

What You’ll Need

  • A Linux system (Ubuntu, Debian, Fedora, CentOS, Arch, etc.)
  • Terminal access with sudo or root privileges
  • Internet connection (to install packages)
  • Files or directories you want to compress or extract

Step-by-Step

  1. Install p7zip
    Open a terminal and run the appropriate command for your distribution:
    • Ubuntu/Debian: sudo apt update && sudo apt install p7zip-full p7zip-rar
    • CentOS/RHEL: sudo yum install p7zip p7zip-plugins or sudo dnf install p7zip p7zip-plugins
    • Fedora: sudo dnf install p7zip p7zip-plugins
    • Arch Linux: sudo pacman -S p7zip
    After installation, verify it works by typing 7z and pressing Enter—you should see the help summary.
  2. Compress a single file into a .7z archive
    Use the command: 7z a archive-name.7z filename
    Example: 7z a backup.7z report.pdf
    This creates backup.7z containing report.pdf. By default, it uses maximum compression (level 9).
  3. Compress a directory recursively
    The same a (add) command works for folders: 7z a project-backup.7z /home/user/project
    This archives the entire project directory and all its contents, preserving subdirectories.
  4. Specify compression level and options
    Add flags for control:
    • Fast compression: 7z a -mx=1 archive.7z folder/
    • Maximum compression: 7z a -mx=9 archive.7z folder/
    • Set dictionary size and memory usage: 7z a -m0=lzma2:d=64m:mc=32 archive.7z folder/
    The -mx= flag accepts values 0 (store only) through 9 (ultra).
  5. Encrypt with a password
    To password-protect your archive, use: 7z a -p archive.7z data/
    You’ll be prompted to enter and confirm the password. Note: the password isn’t echoed to the terminal for security. For *strong* encryption, rely on the default AES-256 used by 7z format—even if you use -p, the 7z format encrypts both file contents and filenames.
  6. Extract an archive
    To extract to the current directory: 7z x archive.7z
    To extract to a specific folder: 7z x archive.7z -o/home/user/restore
    Use 7z e instead of x if you want to extract files *without* preserving directory paths (flat extraction). The -o flag must be immediately followed by the path with no space (e.g., -o/output).
  7. List archive contents without extracting
    Run: 7z l archive.7z
    This shows file names, sizes, compression ratios, timestamps, and attributes—very useful for verifying contents before extraction.
  8. Update or delete files in an existing archive
    To add a new file to an existing archive: 7z u archive.7z newfile.txt
    To delete a file from an archive: 7z d archive.7z oldfile.txt
    These commands modify the archive in place, so keep backups if the archive is critical.

Common Mistakes

  • Missing the -o space
    Using -o /path (with a space) causes 7z to interpret /path as a file name, not an output directory. Always write it as -o/path with no space.
  • Confusing x and e
    The x command preserves folder structure; e flattens it. If you expect subfolders and get all files in one place, you likely used e by mistake.
  • Forgetting to install p7zip-full
    On Debian/Ubuntu, p7zip alone lacks full 7z support. You *must* install p7zip-full for .7z compression and p7zip-rar for RAR extraction.
  • Overlooking archive format differences
    ZIP archives created with 7z use its own compression, but standard ZIP tools may not support AES encryption or solid blocks. For maximum compatibility, use zip for simple ZIPs; use 7z for .7z or advanced ZIP features.
  • Using -p with ZIP archives
    The -p flag only encrypts file *contents* for ZIPs—not filenames. For full encryption (including filenames), stick to the .7z format.

Tips

  • Use -mmt=on for multi-threading
    Speed up compression/decompression on multi-core CPUs: 7z a -mmt=on archive.7z folder/. You can specify core count (e.g., -mmt=4).
  • Split large archives automatically
    Create multi-volume archives: 7z a -v100m archive.7z folder/ creates archive.7z.001, archive.7z.002, etc., each 100 MB.
  • Test archive integrity before trusting it
    Run 7z t archive.7z to verify all files can be extracted without errors—especially important for backups.
  • Preserve file timestamps
    By default, 7z preserves modification times. To also preserve access times, use -mta=on (added in newer versions).
  • Batch compress multiple folders
    Loop through directories: for dir in */; do 7z a "${dir%/}.7z" "$dir"; done creates one archive per subdirectory.

Troubleshooting

  • Command not found
    If you see 7z: command not found, the package isn’t installed. Re-run the install command and ensure p7zip-full is included. On Ubuntu, apt list --installed | grep p7zip confirms installed packages.
  • Unsupported format error
    Trying to extract a .rar or .tar.gz may fail if plugins are missing. Install p7zip-rar (Debian/Ubuntu) or p7zip-plugins (RHEL/Fedora) to add support.
  • Password prompts not appearing
    If 7z x -p doesn’t ask for a password, the archive may be unencrypted, or you may have mis-typed the flag. Use 7z l archive.7z to check if it’s password-protected (look for “Encrypted = +”).
  • Permission denied errors
    If extraction fails with permission issues, ensure you own the output directory: sudo chown -R $USER:$USER /target/path or run extraction with sudo (if safe).
  • Corrupted archive
    If 7z t reports errors, try extracting with 7z x -bb3 archive.7z (ignores minor errors) or use 7z x -y archive.7z to force overwrite. If the archive is vital, restore from backup.

Frequently Asked Questions

Is 7z better than ZIP or tar.gz?

7z typically achieves higher compression ratios than ZIP and often beats tar.gz (gzip), especially for similar data. However, ZIP is more universally supported, and tar.gz is standard in Unix/Linux ecosystems for backups and source archives. Choose based on compatibility needs versus compression efficiency.

Can I extract RAR files with 7z?

Yes—if you’ve installed p7zip-rar (Debian/Ubuntu) or p7zip-plugins (RHEL/Fedora). Without them, 7z won’t handle RAR. Note: only older RAR formats (v1.5–v4.x) are fully supported; newer RAR5 may not work.

Does 7z preserve symbolic links?

By default, 7z stores symbolic links as links (not the target files), but only if you use the -ms=on flag. To archive the *target* of symlinks instead, use 7z a archive.7z -ms=off folder/. Always verify with 7z l before relying on symlink behavior.

How do I compress without deleting the original files?

7z never deletes source files during archiving—the a command only adds copies to the archive. Your original files remain untouched unless you manually delete them later.

Can I use 7z over SSH for remote backups?

Yes. Example: ssh user@remote '7z a -stdout /path/to/data | gzip -c' > local-backup.gz compresses and streams data over SSH. For large transfers, combine with -v to split archives and reduce memory usage.

What’s the difference between 7z, 7za, and 7zr?

7z is the full-featured version (includes all plugins). 7za is a standalone binary with fewer format supports but no external plugins. 7zr is a reduced version of 7za that only supports 7z archives. On most modern Linux distros, 7z is the default command after installing p7zip-full.