How to install and use 7-zip in Linux

7-zip is a free, open-source file archiver that uses the 7z file format (extension). However, this tool can read and write to other file formats. 7-zip is most popular among Windows users. It can run both in the command-line or the Graphical User Interface to read and write archive files. Even though 7-zip is free and open-source and the code is under the GNU LGPL license, the unRAR code has unRAR restriction.

The restriction is put in place to prevent reverse engineering of the RAR compression algorithm. 7-Zip archive format is not supported by default in most Linux distributions. If you try to extract a 7-zip archive file, you may get the following error:

Error extracting 7z archive
Error extracting 7z archive

However, this shouldn’t worry you. In this post, we will give you precise detail on how to install 7-Zip on Linux and how to use it to extract and archive files.

When we execute a command like apt install 7-zip on the Ubuntu Terminal, you will get an error like: Unable to locate package 7-zip. That is because, in Linux, 7Zip is packaged as p7zip.

Install 7-Zip in Linux

As detailed above, p7zip is the package format for 7-Zip in Linux systems. The p7zip is available in three versions -p7zip, p7zip-full, and p7zip-rar. p7zip and p7zip-full are closely related. The only difference is p7zip-full provides support for more 7z compression algorithms. p7zip provides support for p7zip-rar files. For a better experience, we will install p7zip-full and p7zip-rar.

Install 7-Zip on Ubuntu

1. Launch the Terminal from the applications menu or using the keyboard combination Ctrl + Alt + Z.

2. Enable the universe repository in Ubuntu. That is the repository that holds p7zip packages.

sudo add-apt-repository universe
sudo apt update

Enable Universal repository
Enable Universal repository

3. Install 7zip in Ubuntu.

sudo apt install p7zip-full p7zip-rar

Install p7zip-full p7zip-rar
Install p7zip-full p7zip-rar

Install 7-zip in Fedora or CentOS / RHEL

Launch the Terminal and execute the command below.

sudo yum install p7zip p7zip-plugins

Install P7zip Fedora
Install P7zip Fedora

Now that we have successfully installed 7-Zip in our Linux systems, let’s look at ten useful p7zip commands.

10 useful 7-zip Linux commands

1. Create a ‘.7z’ archive file:

We will use the “a” argument. Other supported formats include XZ, GZIP, TAR, ZIP, and BZIP2. If an archive with the same name already exists, 7zip will append the library files instead of overwriting it.

Syntax:

7z a [output-file-name] [file-to-archive]
e.g
7z a myArchive.7z StudyModules/

Create a 7z archive
Create a 7z archive

2. Extract a ‘.7z’ archive:

We will use the “e” option, as shown in the command below.

Syntax

7z e [archived-file.7z]
e.g.,
7z e myARchive.7z

Extract a 7z Archive
Extract a 7z Archive

3. Use a specific archive format:

To do so, we will use the “-t” option. The archive formats you can use include 7z(default), gzip, zip, tar, or bzip2.

Syntax:

7z a -t[format-option] [file-to-archive]
e.g.,
7z a -tzip myArchive.zip StudyModules/

Archive to a specific format
Archive to a specific format

4. List files in archive:

To see all the files in an archive, the archive format used, the method used, and other important information, we will use the “l” option,

Syntax:

7z l [archive-file]
e.g.,
7z l myArchive.zip

List files in an Archive
List files in an archive

5. Test the Integrity of an Archive:

To verify the integrity of an archive, we will use the “t” option.

Syntax:

7z t [archive-file]
e.g.,
7z t myArchive.7z

Test Archive Integrity
Test Archive Integrity

6. Backup a directory.

The best to create a backup would be to use the 7za utility. It preserves the owner/group o a file, unlike the 7z. We will also include the “-si” option that enables reading files from stdin (Standard Input Stream).

We will first create a tar archive then create a 7z backup archive with the 7za utility.

tar -cf - StudyModules | 7za a -si StudyModules.tar.7z

Create a Backup
Create a Backup

7. Restore a Backup with 7zip.

We will use the -so option with the Standard Output Stream (stdout).

7za x -so StudyModules.tar.7z | tar xf -

Restore Backup
Restore Backup

8. Set a custom compression level.

We will use the -mx option. The compression level ranges from 1 to 10, with 1 being the default option. The larger the number, the better the compression ratio, but the longer the compression process time.

tar -cf - StudyModules | 7za a -si -mx=9 StudyModules.tar.7z

Set compression level
Set compression level

9. Add/Remove files to an archive (Update).

To add new files or update an archive, we will use the -u option.

Syntax:

7z u [archive-name] [files-to-add]
e.g.,
7z u myArchive.7z ceh-V1.pdf ceh-V2.pdf

Update Archive
Update Archive

To remove files from an archive, we will use the -d option.

Syntax:

d [archive-name] [files-to-remove]
e.g.,
7z d myArchive.7z ceh-V1.pdf ceh-V2.pdf

Delete files from an archive
Delete files from an archive

10. Set a password for an archive.

We will need to use the -p flag.

Syntax:

7z a -p{your-password} [archive-file-name] [files-to-archive]
e.g.,
7z -a -p{admin123} myArchive.7z StudyModules/

Set a password for an Archive
Set a password for an Archive

Conclusion

That is a complete guide on how to install and get started with 7zip on Linux. You can look at the 7z man page or visit their official website for more information. Use the feedback section below to ask any questions or share any additional information with our readers.

Leave a comment

Your email address will not be published. Required fields are marked *