How To Use ‘7z’: The Complete Linux Command Guide
The 7z format is used increasingly when downloading software or simply organizing files on Linux. It’s a great tool with many features, but you might need a hand to use it properly and remember the command options. You’ve come to the right place, let me tell you everything!
7z is a file archiver that allows the grouping of files and directories in compressed files called “Archive” which can be divided into parts. Through compression algorithms, all the archives can achieve high efficiency, reducing their size and offering security through strong encryption.
In this article, you will learn how to use 7z to compress and decompress files, protect them with a password, and even manage compressed files for better file management.
If you need help with Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your system. Click here to get it for free!
Installing 7z
Unlike many file compressors, 7z is not included in most Linux distributions by deafult. However, it is usually included in their repositories, so you can easily install it using any package manager.
In my case, I will be using Ubuntu, so I can install it using APT:sudo apt install p7zip-full
The command and the package used for 7z might differ for other distributions. Here you can see the different cases in the common distributions:
- Red-Hat/CentOS Based Distributions:
sudo dnf install p7zip
- Arch Based Distributions:
sudo pacman -S p7zip
If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.
Command Syntax
The typical syntax of 7z looks like:7z <function> <switch> <file(s)>
Using the<function> option will indicate the action like compression or manage and <switch> will specify certain features or characteristics depending on the command, like passwords or splitting parts.
Usage Examples
Let’s take a few examples to make this more concrete.
Compress Files/Directories to an Archive
When we talk about file compression, we refer to reducing the size of a dataset (files/directories) and grouping data into a single compressed file called “Archive” with 7z. The process of reducing these files and grouping them in this format is called compression.
To compress files, you can use the command 7z followed by “a” and the files to include, something like:7z a <archive-name>.7z <file1> <file2>
If you want to compress a whole directory, it’s possible to specify the directory instead of the name of the file at the end of the command:7z a <archive-name>.7z <directory>
Viewing the content of an Archive
Seeing the content of a compressed archive in 7z can save you time and disk space before extracting the files and you will be able to edit the content.
Also: I tried to replace my main PC with a Pi 5, here's what happened.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
To list the contents of an archive you can use 7z with “l“:7z l <archive-name>.7
The listing will not only show you files, but it will also tell you the size, metadata, compression ratio and timestamps, quite detailed information you may need.
Decompress Files/Directories from an Archive
Decompressing is restoring a compressed file to its original state. It’s the reverse process of compressing.
To decompress in 7z, you need an archive compressed in 7z format and enough space in the disk used to extract the files.
The function name in this case is “x”, and the command to extract the contents of an archive looks like:7z x <archive-name>.7z
Note: 7z can decompress more formats than just 7z, but you need to consult the official website before doing it.
In case you want to be more specific, even in the output directory where you want the extracted files to be saved, you can specify it in the decompress command at the end, like this:7z x <archive-name>.7z -o<output-directory>
There is another command to decompress, where the files will be extracted without a directory.
Let’s see, for example, the content of this archive:
If you look at the archive’s contents, you will notice a folder with several files. If using 7z “e” instead of 7z “x”, only the files will be decompressed without a structure. The command will remain the same except for the function.7z e <archive-name>.7z
Adding and Removing Files/Directories in an Archive
You can add new content or delete the already existing in a compressed archive, there are two commands that you can use “u” (update) or “d” (delete).
Let’s take a look at this example first. I already have an archive called rpitips-archive.7z that contains several files inside.
If we want to add a new file, you can use the command 7z with “u” to do it:7z u <archive-name>.7z <file-to-add>
Now, to remove an unwanted file in the compressed archive, you can use the command 7z “d”:7z d <archive-name>.7z <file-to-delete>
Modifying the archive’s content allows you to fix errors, reduce the decompress times, and handle file management efficiently.
Main Options
Functions
Here’s a summary of all the functions that you can use depending on your needs:
- a Add files to archive.
- d Delete files from the archive.
- e Extract files from the archive (without using directory names).
- l List contents of the archive.
- rn Rename files in the archive.
- t Test the integrity of the archive.
- u Update files to the archive.
- x eXtract files with full paths.
Switches
Now, based on the functions that you use, you can use switches too, which can give you advanced options for your function:
- -p{Password}: Protect your archive with a password. (Compress)
- -o{Directory}: Specify the output directory for extracted files. (Decompress)
- -t{Type}: Set the archive type (e.g., 7z, zip, gzip, bzip2, tar). By default, the archive type is 7z. (Compress)
- -v{Size}[b|k|m|g]: Create split volumes of a specified size. (Compress)
- -sfx[{name}]: Create a self-extracting archive. (Compress)
- -x[r[-|0]]]{@listfile|!wildcard}: Exclude specific files or patterns. (Compress/Decompress)
- -bd: Disable the percentage indicator.
- -y: Automatically answer “Yes” to all prompts. (Compress/Decompress)
Tips
Protect your Archive with Password-Protection
You can add a password to your archive when creating a 7z archive. You can do this by using the switch “-p” followed by your password. The files won’t be extracted without the correct password, so you must know your password before decompressing.
When compressing the files, you must use the switch <p> together with the command to compress, like this:7z a -p<password> <archive-name>.7z <files>
If you want to decompress this file, you will be asked for a password. But overall, the command doing this is the same one used for decompressing files, using either x or e:
7z x <archive-name>.7z
Remember to use a secure password that is not easy to guess. You can use this password generator to generate a random password that works perfectly to guarantee you protect your compressed files.
Also: Need a clear GPIO pinout diagram? Here's mine.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Split your Archive into parts
You can split your compressed files into parts to make them easier to share or download. This allows you to have a compressed archive of 1GB, or 10 parts of 100MB.
To split the compressed files, specify the “v” switch during the file compression process. In which you can specify the size of each part:7z a -v<b/k/m/g> <archive-name>.7z <file>
Note: You can choose between b= byte, k= kilobyte, m= megabyte or g= gigabyte.
After the compression, the file will be split into equal parts, except for the last one, which will be saved with the remaining size. The sum of all these parts equals the total of a single compressed file.
If you want to decompress your split archive, you need to decompress the first file and the rest will be decompressed automatically.
Alternatives to 7zip
Of course, 7zip isn’t the only way to create and manage archives on Linux, so here are some other options you’re likely to come across that you might want to consider for your archives.
tar
Tar (tape archive) is a tool that is installed in most distributions. Unlike other file archives, tar does not compress the files, it only takes care of grouping them and works together with gzip and bzip2 to perform this process.
Its main benefit is that tar keeps the structure and metadata of the files and directories. It’s an excellent option if you need both features. Compared to 7z, its compression is null, and it does not have the rest of the benefits that 7z has.
If you want to know more about tar, we already have an article that will explain a lot of things about tar.
zip
This is another of the most common tools found for grouping files. Unlike tar, zip does offer file compression, therefore, you can reduce the file size once it is compressed, besides having basic security protection in case you need it.
If we compare it with 7z, the compression of zip is quite smaller so your compressed files can be bigger, and the compression process can be slower in case there is a big amount of files.
Read more: How To Use ‘zip’: The Complete Linux Command Guide
rar
rar is one of the most popular compression tools. It offers higher levels of compression, better performance, and stronger password encryption to secure the files.
The only disadvantages are that the software is not open-source, so you will have to pay to use it, and you may find it less available in Linux (since it is mostly used on Windows).
Note: Although unrar is available for free on Linux, it is only for decompressing.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Related Questions
How can I be sure that the files in my Archive are still intact?
You can verify a compressed file by simply using the 7z <t> command and choosing the compressed file that you want to verify:7z t <archive-name>.7z
Keep in mind that you should always check the integrity of the compressed file every time you transfer or move it to another folder or device. It’s easy for files to get corrupted if the transaction doesn’t work well, and it is better to notice it before decompressing it.
What are the differences between the 7z, zip, and tar formats?
The main difference that you will find in the 3 files will be their size, and it will be more noticeable in the case of tar, since it does not compress files but only groups them.
In the case of zip, the size will be bigger than 7z but smaller than tar because its compression level is lower than 7z, but it performs the compression function that tar lacks.
If we compare 7z and zip files, we consider the tool being used to decompress, since not all tools are compatible with the files.
How do I decompress a specific file or directory from an Archive?
To save time and OS resources, before performing the decompress process, you can choose which file or directory you want to specify to extract from the compressed archive.
Before choosing which file you want to decompress, you must know the content of the compressed archive. We already learned that with 7z “l” we can see the content inside an archive.
After knowing the content of the archive, you can choose the file to decompress by simply choosing it at the end of the command:7z x <archive-name>.7z <file-to-decompress>
As you may notice, once you do this, you will save a lot of time and space on the disk, since only the chosen file will be extracted.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!
Additional Resources
Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.
VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.