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
- 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-pluginsorsudo dnf install p7zip p7zip-plugins - Fedora:
sudo dnf install p7zip p7zip-plugins - Arch Linux:
sudo pacman -S p7zip
7zand pressing Enter—you should see the help summary. - Ubuntu/Debian:
- 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 createsbackup.7zcontainingreport.pdf. By default, it uses maximum compression (level 9). - Compress a directory recursively
The samea(add) command works for folders:7z a project-backup.7z /home/user/project
This archives the entireprojectdirectory and all its contents, preserving subdirectories. - 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/
-mx=flag accepts values 0 (store only) through 9 (ultra). - Fast compression:
- 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. - 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
Use7z einstead ofxif you want to extract files *without* preserving directory paths (flat extraction). The-oflag must be immediately followed by the path with no space (e.g.,-o/output). - 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. - 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
-ospace
Using-o /path(with a space) causes 7z to interpret/pathas a file name, not an output directory. Always write it as-o/pathwith no space. - Confusing
xande
Thexcommand preserves folder structure;eflattens it. If you expect subfolders and get all files in one place, you likely usedeby mistake. - Forgetting to install p7zip-full
On Debian/Ubuntu,p7zipalone lacks full 7z support. You *must* installp7zip-fullfor .7z compression andp7zip-rarfor RAR extraction. - Overlooking archive format differences
ZIP archives created with7zuse its own compression, but standard ZIP tools may not support AES encryption or solid blocks. For maximum compatibility, usezipfor simple ZIPs; use7zfor .7z or advanced ZIP features. - Using
-pwith ZIP archives
The-pflag only encrypts file *contents* for ZIPs—not filenames. For full encryption (including filenames), stick to the .7z format.
Tips
- Use
-mmt=onfor 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/createsarchive.7z.001,archive.7z.002, etc., each 100 MB. - Test archive integrity before trusting it
Run7z t archive.7zto 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"; donecreates one archive per subdirectory.
Troubleshooting
- Command not found
If you see7z: command not found, the package isn’t installed. Re-run the install command and ensurep7zip-fullis included. On Ubuntu,apt list --installed | grep p7zipconfirms installed packages. - Unsupported format error
Trying to extract a .rar or .tar.gz may fail if plugins are missing. Installp7zip-rar(Debian/Ubuntu) orp7zip-plugins(RHEL/Fedora) to add support. - Password prompts not appearing
If7z x -pdoesn’t ask for a password, the archive may be unencrypted, or you may have mis-typed the flag. Use7z l archive.7zto 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/pathor run extraction withsudo(if safe). - Corrupted archive
If7z treports errors, try extracting with7z x -bb3 archive.7z(ignores minor errors) or use7z x -y archive.7zto 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 installedp7zip-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—thea 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.