In this blog post, we will explore how to use 7-Zip Compression Tool from Linux Terminal
7-Zip is a widely used compression utility that supports a variety of archive formats, including 7z, ZIP, GZIP, TAR, Zip64, CAB, RAR, ARJ, BZIP2, CPIO, RPM and more. Developer claims that the compression ratio in the new 7z format is 30-50% better than the ratio in Zip formats.
Install 7zip in Linux
The 7zip utility is not pre-installed on the Linux systems. You have to install it using the following command.
For Ubuntu / Debian / Linux Mint
$ sudo apt install -y p7zip-full
For RHEL / Fedora / Rocky Linux / Alma Linux
sudo yum install p7zip p7zip-plugins -y or sudo dnf install p7zip p7zip-plugins -y
NOTE: In case, you want to use 7zip with rar files, then you’ll have to install the following package as well.
$ sudo apt install p7zip-rar -y
Without any delay, let’s jump into 7zip command usage with different practical examples.
Basic Usage of 7-Zip
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>..
To compress files or folders, you can use the 7z command, followed by the archive name and the files you want to compress.
We will be using the files displayed in the following screenshot for performing various operations using 7zip.
Compressing Files/Folders
Using this 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 *
Now to check, whether the archive file is created or not, you can use the ls 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.
Note: By default 7zip creates an archive with format .7z, we can change this format with option -t as shown below.
$ 7z a -tzip testfiles.zip *
Listing 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.
Deleting 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
Extracting Files from Archive
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.
Here’s how that can be done:
$ 7z e testfiles.7z
Updating Existing Archived File
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.
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
Testing Integrity of Archived File
With 7zip 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
Above output shows that the contents of the archive are not corrupted.
Advanced 7-Zip Options
7-Zip provides several advanced options and parameters that you can use to customize your compression and extraction processes. Some common options include setting compression levels, encryption, and more. For instance, to create a password-protected archive, you can use the -p option:
$ 7z a -p my_encrypted_archive.7z my_folder
You’ll be prompted to enter a password for the archive.
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 blog post 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.
Also Read: Tar Command in Linux with Practical Examples
Thanks for the helpful tutorial
thanks for the good article
but I still don’t know how to make a RAR archive with 7z
7zip tool has a great compression ratio for my files and offers higher compression than RAR, ZIP, and other formats…
how about an explanation of how to extract specific files or directories from 7z archive with the terminal. Many headaches. Help a Noob out.