Connect with us

Linux

How to unzip a zip file on Linux [2020]: The Ultimate Guide

Let’s Learn How to unzip a zip file on Linux  with Practical Examples :

In this guide, you will learn how to unzip a zip file on Linux. So what’s a zipped file? This is a file that is compressed to save on the space it occupies. In addition, zipped files are easier to send as email attachments and also to download from websites.

You can identify a zipped file using the .zip file extension. Unzip is a command-line utility used to unzip or decompress files with a .zip file extension. Let’s have a look at various ways you can use and how to unzip a zip file on Linux using those methods.

In summary, we will cover the following:

  1. Install unzip command in Linux
  2. Unzip a zip file on Linux
  3. How to zip files/directories in Linux
  4. Install zip command in Linux

Install unzip command in Linux

While the unzip package may come installed in most modern Linux distributions, it may not ship with some Linux flavors by default. But that’s not a big issue. You can easily install the unzip package as shown:

Install unzip in Ubuntu/Debian distributions

If you are working on a Debian-based system, you can install zip by running the command:

Install unzip in RHEL / CentOS

If you are running a RedHat-based distribution such as CentOS and RedHat, execute the commands below to install unzip:

Install unzip in Fedora

For fedora systems, execute the command below:

Install unzip in ArchLinux / Manjaro

For Arch-based systems such as Manjaro, install unzip using the Pacman package manager as shown:

Install unzip in OpenSUSE

To install unzip in OpenSUSE systems, run the command:

How to unzip a zip file on Linux

In its most basic form, without any arguments, the unzip tool is used for unzipping files in Linux. The basic syntax is shown below:

For example, to unzip a file called nextcloud-18.0.3.zipdownloaded from Nextcloud, execute the command:

Note that the user who has unzipped the file owns the extracted directory and files therein. For example, if a user ‘Winnie’ has unzipped a file, then the directory and all the extracted contents will bear Winnie’s ownership rights as shown in the images below.

unzip a file in Linux

In addition, you must have the requisite permissions needed to unzip a file that is not owned by the user. Otherwise, if you are a regular user, make use of the Sudo command to get administrative privileges to unzip the file.

Unzip multiple files in one command

If you have several zipped files in a directory, you can unzip them simultaneously using one command. This is done using a wildcard symbol (*). The syntax is shown below:

Unzip files to a different directory

You can unzip a file to a different directory using the -d option followed by the destination directory as shown:

Using our Nextcloud file, we are going to unzip it to the /var/www/html directory which is the webroot directory. To achieve this, run:

Note that this time we have used sudo because the regular user ‘Winnie’ does not have write permissions to the destination. When unzipped, the uncompressed files are in effect owned by the root user.

Suppress verbose output of unzip

Usually, when unzipping a file, the extracted files are displayed on the terminal during the unzipping process. You can choose to omit the display of these files using the -q option as shown:

For example:

suppress output of unzip in Linux

Overwrite an existing unzipped file

If you already have an unzipped file and you messed or deleted a few files, you can unzip the zipped file again to overwrite the existing unzipped file. Usually, you will be prompted whether you want to overwrite the current file, whether you’d like to skip overwriting it or whether you want to replace all the files.

To suppress such prompt, use the -o flag to proceed with

Unzip a file in Linux without overwriting

There are scenarios that you may want to skip overwriting a file(s). Let’s assume you have an unzipped file containing several files. You make some changes to some of the extracted files, however, something goes wrong and you lose some of the files due to accidental deletion. In this case, you may be compelled to unzip the file again, but this time, retain the changes you made to some of the files.

To avoid overwriting the changes, use the -n flag as shown:

The outcome will be the omission of the extraction of preexisting files. You will get your deleted files back but the changes you made to some of the files will remain intact.

List contents of a zipped file

You can always have a peek to verify the contents of a zipped file. This is achieved using the -l option as shown:

For example:

On your terminal screen, you will have the contents of the zipped file listed as shown:

Unzip tarball files

Thus far, we have looked at how you can extract or unzip files with a .zip file extension. In this section, we look at how you can extract tarball files.

A tarball file is an archive file that has been compressed using the tar command. TAR, short for tape archive. We already have an article on how you can use tar command in basic Linux commands. Tarball files usually have a .tar.tgz, .tar.bz2, tar.xz or tar.gz

To unzip a tar xz file, tar tgz file or tar bz2 file, use the syntax below

Let’s take a look at the options used:

x – This instructs the tar command to extract the file

v – This prints verbose output on the screen

f – This invokes the file name

For example, to unzip to extract a file latest.tar.gz, execute the command:

How to zip files/directories in Linux

In this section, we will see how we can zip a file or a directory. To create a zip file, we use the zip command-line utility. The basic syntax is shown below

The zip utility comes installed in most major and popular Linux distros. While that may be the case, should you encounter a system without the zip tool installed, here’s how you can install the command-line tool.

How to install zip command in Linux

In this section, we are going to look at how to install unzip command in various Linux distributions.

Install zip in Ubuntu/Debian distributions

For Debian systems, you can install zip by running the command:

Install zip in RHEL / CentOS

For Redhat-based Linux systems, install zip by running the command:

Install zip in Fedora

Again, for Fedora, use the dnf package manager to install zip as shown below.

Install zip in ArchLinux / Manjaro

For Arch-based systems like Manjaro, Install zip by executing the command:

Install zip in OpenSUSE

To install unzip in OpenSUSE systems, run the command:

Let’s now shift gears and check out a few examples of how you can unzip files in Linux.

Zip files in Linux

Quite notably, zip is the opposite of unzip command. This involves creating a tarball file or archive. Zipping a file on the terminal is quite easy and takes the following syntax:

The above command zips a single file into a zipped archive. Alternatively, you can zip multiple files into one zip archive as shown:

Zip directories in Linux

Often times, you will be required to zip a directory to save on the hard disk space on your system. To achieve this, use the -r option as shown.

On top of that, you can include multiple directories and files into the zip archive as shown:

And this concludes our tutorial on how you can unzip a zip file on a Linux system. Your feedback is most welcome.

Continue Reading
Advertisement

Trending