The 7zip program is a file archiver (a program that supports multiple files in the same archive) that uses the lzma compression algorithm. It’s native file format is the .7z
file, and additionally it supports a huge set of file formats, including:
- Packing/unpacking: 7z, xz, bzip2, gzip, tar, zip, wim
- Unpacking only: ar, arj, cab, chm, cpio, cramfs
Other key features are:
- For ZIP and GZIP files, it promises a better compression ratio than standard programs, like pkzip and WinZip
- AES 256 encryption in 7z and ZIP formats
- Self-extracting capability for 7z format
- Localization for 80+ languages
Table of Contents
Installing 7zip on Linux
There are two ways of installing 7zip on your system. The easiest (and preferable) way is using your distro’s package system. If the package is not available, you can download a pre-compiled version at 7zip site’s, https://www.7-zip.org/
Install 7zip Using a Package Manager
Debian-based distros (like Debian and Ubuntu):
apt-get -y install p7zip
Red Hat based distros (Fedora, CentOS, AlmaLinux, Rocky Linux):
yum -y install p7zip
Manually Installing 7zip
To manually install 7zip access the project’s download page at https://www.7-zip.org/download.html and download the versions corresponding to your system ( on most systems it will be the 64-bit Linux x86-64 version). You can use wget
(Debian/Ubuntu) or curl
(Red Hat/CentOS) command for downloading (the name of the file may be different, due to version updates):
curl -LOJ https://www.7-zip.org/a/7z2107-linux-x64.tar.xz
or
wget https://www.7-zip.org/a/7z2107-linux-x64.tar.xz
After the download, extract the 7zzs file from the tar package with the command:
tar xf 7z*-linux-x64.tar.xz 7zzs
Test the extracted file with the following command:
./7zzs --help | head
If everything is Ok, a similar output will be shown, with the program version and usage.
At this point the program is ready to use, but, for convenience, you should install it on a directory on the system’s path, so other people can use it and you can only type the program name (without the “./” part). A typical place to put the file is in directory /usr/local/bin (you need root permissions, so execute the command as root or with sudo):
cp 7zzs /usr/local/bin
or
sudo cp 7zzs /usr/local/bin
It’s more convenient to set an easier name for the program. So we can create a symbolic link to the program, called for example, 7zip, or other name of your preference. Indeed, you can even create multiple links with different names to the same program. You’ll also need root/sudo permissions for this step:
ln -s /usr/local/bin/7zzs /usr/local/bin/7zip
or
sudo ln -s /usr/local/bin/7zzs /usr/local/bin/7zip
Note: the downloaded file also contains a user manual in HTML format (for viewing on a web browser). You can extract it with the following command:
tar xf 7z*-linux-x64.tar.xz 7zzs MANUAL
Using 7zip
The command line options are made of one command (without a -
character) and, depending of the command, zero or more switches (preceded by a -
character) and file/directory/listfile specification.
7zip <command> [<switches>...] <archive_name> [<file_names>...] [@listfile]
Wildcards
*
means a sequence of any character
?
means any character, once
Recursiveness
Some switches deal with recursiveness (going deep into the directory tree, looking for files to compress). Recursiveness can be defined in three ways:
-r
: enable recursion on subdirectories.-r-
: disable recursion on subdirectories (default behaviour).-r0
: enable recursion subdirectories only for wildcards
Listfiles
A listfile is a file containing a list of files or wildcards for being processed, one file per line. They are informed on command line by a preceding @ signal before filename.
Example: You can create a file named backup.txt with the following content:
/etc /home /var/www
To use it, you can just run a command like:
7zip a backup.7z @backup.txt
Main Commands
a
: add files to archive. The file must not exist in the archive. To update existing files, use the update command (u
).l
: list archive contentsi
: show info about supported formatst
: test integrity of archiveu
: update files to archive (newer versions of already existing files)x
: extract files with full path
Main Switches/Options
--
: stop processing options and listfiles (everything ahead will be treated as a file or directory, not command options).-ai[recurse type]<file ref>
: define files to include on the executed operation. For recurse type, see recursion above. File ref is either a @listfile or a wildcard.-ax[recurse type]<file ref>
: define files to exclude from the executed operation. For recurse type, see recursion above. File ref is either a @listfile or a wildcard.-ao[a|s|t|u]
: controls the overwrite of files on extraction. -aoa: overite all files without prompt. -aos: skip extracting existing files. -aou: auto rename the extracted file. -aot: auto rename the existing file.-bb[0-3]
: define log verbosity-bt
: show time statistics.-db
: disable progress indicator-m<method params>
: set the compression method. The values of this switch depends on the archive file type. Check the command manual for more info.-mmt[N]
: set number of CPU threads-mx[N]
: set compression level from -mx1 (less compression, faster) to -mx9 (ultra compression, slowest)-r[mode]
: recurse mode (see recursion above)-o<dir path>
: specify a destination directory for the extracted files.-p<password>
: specify a password, both for reading an encrypted archive or for creating one.-sdel
: remove files from disk after compressing them.-si
: reads data from standard input.-so
: writes data to standard output.-sfx[module]
: Creates a SFX (Self-Extracting File). The module controls the type of SFX , basically if is GUI (Graphical User Interface) or console (the default option). Check the command manual for more info.-t<Type>
: specify the file type (7z, xz, tar, zip, gzip, bzip2, etc)-v<Size>[b|k|m|g]
: Create volumes of specified size. For example, -v4g will create files up to 4GB, suitable for being burned on DVD media, while -v650m will do the same but up to 650MB (for storing the files in CD media).-y
: assume yes on all prompts (overwriting existing files when extracting).
Exit Status Codes
It is also important to take in consideration the result code of the operation (by checking the value of bash environment variable $?
). It should be 0
(zero), indicating a successful operation. This is especially useful in scripts, to check if an operation was completed witout errors or not. Value different from 0 indicates an error occurred, as per the below table:
0
: No error.1
: Non-fatal error (ex: one or more files could not be compressed because they where locked by another application).2
: Fatal error.7
: Command Line error (invalid synxtax).8
: No enough memory available to execute the operation.255
: User interrupted the process (with Ctrl+C or kill).
Examples of Using 7zip
This adds all files and subdirs from /etc to archive etc.zip (in zip format)
7zip a etc.zip /etc
This adds all files and subdirs from /etc to archive etc.7z (in 7zip format)
7zip a etc.7z /etc
The following lists the contents (plus some information as the amount of files and directories, compression methods and rate, etc) of etc.7z
7zip l etc.7z
The following extracts the etc.7z archive contents on the current dir.
7zip x etc.7z
The following extracts the etc.7z archive contents on /tmp/restoredir directory. The directory will be created if does not exist. Note: there is no space between the -o
and the path (/tmp/restoredir).
7z x etc.7z -o/tmp/restoredir
Conclusion
In this tutorial we covered how to install 7zip on Linux (Debian and RHEL based distros) using a package manager or how to install 7zip manually. We also covered 7zip’s various options, some particularities, and gave a few examples of how to use it. If you have any feedback or issues feel free to let us know in the comments.