How to install and use 7-zip in Linux

7-zip is an open-source app that lets you compress a file or decompress an existing archive file. It supports a wide range of file formats. Here's how to install and use 7-zip on 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.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,241FansLike
394FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

The Ubuntu Cinnamon Remix brings together Linux Mint's Cinnamon desktop with the Ubuntu Core. While some users are welcoming the new flavor of Ubuntu with open arms, others are scratching their heads, wondering where it fits in.
The wait is finally over (almost) for all you Ubuntu fans out there. The latest version of Ubuntu, 20.10 codenamed "Groovy Gorilla," is currently available in the beta version. I have tested out the distro myself, and it is stable enough to take out for a spin.

The 10 Best Free and Open-Source CAD Software

Making its way into the world somewhere in the 1960s, Computer-Aided Design, or better known as CAD, has thoroughly revolutionized design and drawing, constituting a multi-billion market in the US alone. Today, this technology is extensively utilized in engineering-based manufacturing industries and design creations. Products are bought, sold, and traded in millions of Dollars, and this is how CAD flourishes in sectors.

The 10 Best Linux Hex Editors

An Hex editor is a program that allows the user to view, edit, and manipulate binary files, which contain data that is readable by machines, but not humans. This is where hex editors come into play because unlike other programs and tools which interpret data from files, hex editors, on the other hand, offer the user an opportunity to edit the physical binary contents of a file.

Removing the Virus from a Windows PC with a Ubuntu Live USB drive

In this tutorial, we are going to show you how to clean your Windows machine from infected viruses using an Ubuntu live USB or CD and the ClamAV antivirus. The ClamAV is a free, open-source antivirus that can be used on Ubuntu.

15 Tar command in Linux uses with examples

Tar is a famous utility that is basically used for collecting multiple files in a single archive. This file is often called a 'tarball'. Today, we are going to tell you about the different things you can do with tarballs using the tar command, with practical examples.