How to use 7zip Compression tool from Linux Terminal

7-Zip (http://www.7-zip.org/) is a file archiver with highest compression ratio.The tool supports various archive formats such as LZMA2, XZ, ZIP, Zip64, CAB, RAR, ARJ, GZIP, BZIP2, TAR, CPIO, RPM, ISO, most file system images and DEB formats. Developer claims that the compression ratio in the new 7z format is 30-50% better than the ratio in Zip formats.

In this article, we will walk through the practical examples of 7zip utility and their usage.All the below examples have been tested on Ubuntu 16.04 LTS/16.10 and Linux Mint 18 , and the 7zip version we have used is 9.20

Installation

The 7zip utility is not pre-installed on your Ubuntu systems. You have to install it in your system using the following commands.

$ sudo apt-get install p7zip-full

NOTE:In case, you want to use 7zip with rar files, then you’ll have to install the following package as well.

$ sudo apt-get install p7zip-rar

Usage/Functions

In this section, we will discuss the usage of the 7zip tool and the basic features it provides.

Basic syntax of 7zip:

$ 7z [adeltux] [-] [SWITCH] <ARCHIVE_NAME> <ARGUMENTS>..

We will be using the files displayed in the following screenshot for performing various operations using 7zip.

ls-command-output

Create new archive/ add files to existing archive

Using the tool, you can package the files in a .7z file. This feature can be accessed using the ‘a’ function letter.

Here’s how you can use this feature:

$ 7z a [archived-filename] [names-of-files-to-be-archived]

For example:

$ 7z a testfiles.7z *

add-files-in7zip-compressed-file

Now to check, whether the archive file is created or not, you can use the ‘ls‘ command.

add-ls-7zip-command

As you can see in the screenshot above that a .7z archive file (testfiles.7z) has been created. Of course, you can also create/update, say, a .zip file this way.

List contents of archive

You can use the ‘l’ function letter for listing the contents of an archived file.

$ 7z l [archived-filename]

For example:

$ 7z l testfiles.7z

Here is the list of files in testfiles.7z archived file.

listdelete-7zip-command

Delete files from an archive

Using the tool, you can also delete a file from an archive file, something which you can do using the ‘d’ function letter.

$ 7z d [archived file] [name-of-file-to-be-deleted]

For example, to delete testfile1, we can use the following command:

$ 7z d testfiles.7z testfile1

delete-file-from-7zip-compressed-file

Extracting files from archive file

With the help of 7zip, you can extract files from an already existing archive. This feature can be accessed using the ‘e’ function letter.

$ 7z e [archived-file]

For example, suppose we want to extract the ‘testfiles.7z’ archive.

ls-extract-7zip

Here’s how that can be done:

$ 7z e testfiles.7z

extract-7zip-file

Updating existing archived files

If you want, you can add new files to a previously created archived file, or you can even update the contents of existing files in the archive. This is made possible using the ‘u’ function letter.

$ 7z u [archived-file] [name-of-new-or-updated-file]

For example, here is the directory structure in which we will perform some update operations.

directorystructure-linux

Let’s say we have already created an archive of the ‘dir1’ directory. And now, due to some modifications in, let’s say, file1, file2 and file3, we want to update the archive.

So, instead of individually adding the modified files again (using the ‘a’ function letter) to the archive, we can perform the operation in a single run by using the following command.

$ 7z u dir1.7z

update-7zipfile-linux-command-line

Testing integrity of archived files

Using the tool, you can also check the integrity of an already existing archive file. Testing integrity is very important, as it tells, whether the contents are properly archived or not.

Sometimes, due to a hardware problem, RAM problem, or heating problem, your files may get affected and the archive isn’t created properly. To check whether the archive is corrupted or not, you can use the ‘t’ function letter.

$ 7z t [archived-file]

For example:

$ 7z t testfiles.7z

Here is the output

integrity-check-7zip-linux

Above output shows that the contents of the archive are not corrupted.

Conclusion

7 zip tool happens to be one of the best tools for archiving files. It’s available for both Windows and Linux, although only a command line port is available for the latter platform, so a good option for those Linux users whose work involves playing with files on the command line.

What we’ve discussed in this tutorial are only basic features. There are also several advanced options available. Head to the tool’s man page (man 7z) for more information and details.

4 thoughts on “How to use 7zip Compression tool from Linux Terminal”

  1. how about an explanation of how to extract specific files or directories from 7z archive with the terminal. Many headaches. Help a Noob out.

    Reply

Leave a Comment