7-Zip — High-Ratio Archiving with 7z
Practical guide to 7-Zip: pack 7z, ZIP, TAR and more, squeeze files with LZMA2 and protect archives with AES-256 encryption.
7-Zip is an open-source archiver whose LZMA/LZMA2 algorithm often achieves noticeably higher compression ratios than classic ZIP or gzip tools. On the command line it packs and extracts not only its own .7z format but also ZIP, TAR, GZIP, BZIP2 and XZ. This guide walks you through the commands you reach for daily – from a simple archive to AES-256 encryption, multi-volume archives and exclude patterns.
Create Archives
7z a <archive> <files> — Create an archive (format detected from extension).
7z a backup.7z documents/7z a <archive>.zip <files> — Create a ZIP archive.
7z a project.zip src/7z a -mx=<level> <archive> <files> — Set compression level (0=store, 1=fastest, 5=normal, 9=ultra).
7z a -mx=9 best.7z largefile.dat7z a -t<type> <archive> <files> — Specify archive type (7z, zip, tar, gzip, bzip2, xz).
7z a -ttar archive.tar directory/7z a -r <archive> '<pattern>' — Add files recursively matching a pattern.
7z a code.7z -r '*.py'Extract
7z x <archive> — Extract with full paths (preserves directory structure).
7z x backup.7z7z x <archive> -o<dir> — Extract to a specific directory.
7z x backup.7z -o/tmp/restore7z e <archive> — Extract without paths (all files into one directory).
7z e backup.7z7z x <archive> '<file>' — Extract specific files only.
7z x backup.7z 'config.yaml' 'README.md'7z x -y <archive> — Extract and overwrite without prompting.
7z x -y update.7z -o/var/www/List & Test
7z l <archive> — List contents of an archive.
7z l backup.7z7z l -slt <archive> — List with technical details (size, date, attributes).
7z l -slt backup.7z7z t <archive> — Test archive integrity.
7z t backup.7zEncryption
7z a -p <archive> <files> — Create an encrypted archive (prompts for password).
7z a -p secret.7z confidential/7z a -p'<password>' <archive> <files> — Create encrypted archive with inline password.
7z a -p'MySecret123' secret.7z data/7z a -p -mhe=on <archive> <files> — Encrypt file names too (not just content).
7z a -p -mhe=on secret.7z confidential/Split & Update
7z a -v<size> <archive> <files> — Create a split (multi-volume) archive.
7z a -v100m backup.7z large-directory/7z u <archive> <files> — Update files in an existing archive.
7z u backup.7z updated-file.txt7z d <archive> '<file>' — Delete files from an archive.
7z d backup.7z 'temp.log'7z rn <archive> '<old>' '<new>' — Rename a file inside an archive.
7z rn backup.7z 'old-name.txt' 'new-name.txt'Exclude & Common Patterns
7z a <archive> <dir> -x!'<pattern>' — Exclude files matching a pattern.
7z a project.7z src/ -x!'*.log' -x!'node_modules'7z a <archive> <dir> -xr!'<pattern>' — Recursively exclude a pattern.
7z a project.7z ./ -xr!'*.git' -xr!'node_modules'7z a -ttar -so . | 7z a -si backup.tar.7z — Create a .tar.7z archive (tar + 7z compression).
7z a -ttar -so directory/ | 7z a -si backup.tar.7z Conclusion
On the command line, 7-Zip is a Swiss Army knife for archives – but mind the crucial difference between 7z x (restores the full directory structure) and 7z e (extracts every file flat into one directory, which can overwrite same-named files). AES-256 encryption is only available for .7z and .zip archives protected with a password; add -mhe=on to encrypt the file names too. Avoid passing the password inline via -p'…', as it then ends up in your shell history and the process list – the interactive -p prompt is safer.
Further Reading
- 7-zip.org – official project site – downloads, release notes and FAQ
- 7-Zip – command-line documentation – reference for all switches and commands