Install and Use 7zip on Ubuntu 18.04/Ubuntu 20.04

1
9021

In this tutorial, we are going to learn how to install and use 7zip on Ubuntu 18.04/Ubuntu 20.04. 7zip is a free and open-source file archiver with a high compression ratio. The program supports 7z, LZMA2, XZ, ZIP, Zip64, CAB, RAR, ARJ, GZIP, BZIP2, TAR, CPIO, RPM, ISO, most filesystem images and DEB formats. Compression ratio in the new 7z format is 30-50% better than ratio in ZIP format.

The 7zip utility is universal and therefore the example usage shown in this tutorial can be used across any Linux distribution.

Install and Use 7zip on Ubuntu 18.04/Ubuntu 20.04

Installing p7zip package

If the 7zip utility is not already installed in your system, run the command below to install it.

apt install p7zip p7zip-rar p7zip-full -y

If you are running another Linux distribution like CentOS, Fedora or other RHEL based distros, run the command below to install 7zip;

NB: Enable EPEL repository on CentOS/RHEL distributions before you can run this command.

yum install p7zip p7zip-plugins -y

The 7zip utility provides three command line utilities namely, 7z, 7za and 7zr. The difference between the three utilities as outlined in their man pages is that;

  • 7z uses plugins to handle archives.
  • 7za is a stand-alone executable. 7za handles less archive formats than 7z.
  • 7zr is a stand-alone executable. 7zr handles less archive formats than 7z. 7zr is a “light-version” of 7za that only handles 7z archives.

If you would love to use 7zip with rar on Ubuntu and similar distributions, you need to install the p7zip-rar package.

apt install p7zip-rar -y

In this tutorial, we will discuss the usage of one utility, 7z, since the functionality and usage is the same for all the three utilities.

Once the installation is complete, you can verify by running the following command;

whereis 7z 7za 7zr
7z: /usr/bin/7z /usr/share/man/man1/7z.1.gz
7za: /usr/bin/7za /usr/share/man/man1/7za.1.gz
7zr: /usr/bin/7zr /usr/share/man/man1/7zr.1.gz

Command Line Syntax

Now that installation part is done, let’s have a look at command line syntax.

The 7z command line syntax is:

7z <command> [<switch>...] <base_archive_name> [<arguments>...] [<@listfiles...>]

<arguments> can be specified as <switch> | <wildcard> | <filename> | <list_file>
<switch> can be specified in the format; <switch_symbol><switch_characters>[<option>]
<switch_symbol> can be ‘/’ | ‘-‘
<list_file> is specified in the format; @{filename}

NOTE: The expressions within  the square brackets are optional while those within the curly braces have to be substituted by the user.

The following is a brief description of the various commands and switches used with the 7z command;

<Commands>
a: Add files to archive
b: Benchmark
d: Delete files from archive
e: Extract files from archive (without using directory names)
l: List contents of archive
t: Test integrity of archive
u: Update files to archive
x: eXtract files with full paths
<Switches>
-ai[r[-|0]]{@listfile|!wildcard}: Include archives
-ax[r[-|0]]{@listfile|!wildcard}: eXclude archives
-bd: Disable percentage indicator
-i[r[-|0]]{@listfile|!wildcard}: Include filenames
-m{Parameters}: set compression Method
-o{Directory}: set Output directory
-p{Password}: set Password
-r[-|0]: Recurse subdirectories
-scs{UTF-8 | WIN | DOS}: set charset for list files
-sfx[{name}]: Create SFX archive
-si[{name}]: read data from stdin
-slt: show technical information for l (List) command
-so: write data to stdout
-ssc[-]: set sensitive case mode
-t{Type}: Set type of archive
-u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]: Update options
-v{Size}[b|k|m|g]: Create volumes
-w[{path}]: assign Work directory. Empty path means a temporary directory
-x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames
-y: assume Yes on all queries
-an: Disable parsing of archive_name

For a comprehensive list of switches, check the man pages of the 7z, 7zr, 7za or visit the command line switches.

Example Command Usage

Now that we have learnt about some command options and the command line switches, let us see some practical usage of the 7z command. In this examples, we are going to use the following files and directories;

ls
test-dir tfile4.txt tfile5.txt tfile6.txt
ls test-dir/
tfile1.txt tfile2.txt tfile3.txt

Create an Archive

To create an archive, run the 7z command in the format;

7z a test-archive1.7z tfile4.txt tfile5.txt
7z a archive.xz test-dir/

Note that, the supported archive types for creation are 7z, XZ, GZIP, TAR, ZIP and BZIP2. If the specified archive file already exists, it will “add” the files to the existing archive, instead of overwriting it.

Extract Contents of an Archive

To unpack an archive to the current directory;

7z e test-archive1.7z -y

Supported archive types include 7z, XZ, GZIP, TAR, ZIP, BZIP2, LZMA2, CAB, ARJ, CPIO, RPM, ISO and DEB.

Extract the content with full paths

7z x test-archive1.7z -y

List the contents of an archive

7z l test-archive1.7z

Test integrity of archive

7z t test-archive1.7z

Update an archive by adding more files

7z u test-archive1.7z tfile6.txt

Remove some files from an archive

7z d test-archive1.7z tfile4.txt

Example 7z Switches Usage

The examples below are extracted from the man pages of the 7z command.

Add all files from directory “dir1” to archive archive.7z

7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archive.7z dir1

Add all files from directory “dir1” to SFX archive archive.exe

Note that SFX archive MUST end with “.exe”

7z a -sfx archive.exe dir1

 Add all files from directory “a_directory” to the archive “archive.7z” (with data and header archive encryption on)

7z a -mhe=on -pmy_password archive.7z a_directory

For a comprehensive description of 7z switches and example usage, navigate to 7z command line switches.

Backup and limitations of 7z command

DO NOT USE the 7-zip format for backup purpose on Linux/Unix because :

  • 7-zip does not store the owner/group of the file.

On Linux/Unix, in order to backup directories you must use tar :

  • to backup a directory :
tar cf - directory | 7za a -si directory.tar.7z
  • to restore your backup :
7za x -so directory.tar.7z | tar xf -

If you want to send files and directories (not the owner of file) to others Unix/MacOS/Windows users, you can use the 7-zip format. For example:

7za a directory.7z directory
  • Do not use “-r” because this flag does not do what it is expected to do.
  • Do not use directory/* because of “.*” files (example : “directory/*” does not match “directory/.profile”).

That is all about how to install and use 7zip on Ubuntu 18.04/Ubuntu 20..04. Check man pages for more details.

Other Tutorials

Install Foxit PDF Reader on Ubuntu 20.04

Install and Setup ZSH and Oh-My-Zsh on Ubuntu 20.04

Run only Specific Commands with sudo in Linux

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here