This tutorial explains all zip and unzip operations under Linux with practical examples and easy function descriptions.

Installing Zip and Unzip in Linux:

To begin, on Debian based Linux distributions, install both zip and unzip by running the following command:

sudo apt install -y zip unzip

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/1.png610a00dce966e.jpg" data-lazy- height="515" src="data:image/svg xml,” width=”966″>

On Red Hat based Linux distributions (such as CentOS or Fedora), you can install zip by running the command below:

sudo dnf install zip unzip

On ArchLinux or Manjaro run:

Zipping or Compressing Files using Zip:

This first example shows how to zip several files into one .zip file. In the example below, zippedfile.zip is the name I give the new zipped file that contains files linuxhint1, linuxhint2, and linuxhint3. Thus, the zippedfile.zip option is an arbitrary name you can give your compressed, followed by the files you want to zip.

zip zippedfile.zip linuxhint1 linuxhint2 linuxhint3

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/2.png610a00dd85e1e.jpg" data-lazy- height="263" src="data:image/svg xml,” width=”1064″>

Based in the previous example, a new file named zippedfile.zip was created.

The second example shows how to zip similarly named files with a different end or extension. In this case, type the first part of the name shared by all files and replace the individual extension or final part of the name with a wildcard, as shown below.

zip zippedfile2.zip linuxhint*

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/3.png" data-lazy- height="263" src="data:image/svg xml,” width=”1064″>

The following example shows how to compress all files within a specific directory, without including the directory itself. For this, define the path and use a wildcard to refer to all files contained within the directory. In the example below, all files inside the directory linuxhint will be zipped as zippedfile4.

zip zippedfile4 linuxhint/*

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/4.png" data-lazy- height="291" src="data:image/svg xml,” width=”854″>

This example shows how to zip all files in the current directory. Just include all files by using the wildcard, as shown in the screenshot below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/5.png610a00df955f1.jpg" data-lazy- height="263" src="data:image/svg xml,” width=”1064″>

Zipping directories requires the implementation of the -r flag. In this example, the directory named linuxhint will be compressed as zippedirectory.

zip -r zippedirectory linuxhint

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/6.png" data-lazy- height="403" src="data:image/svg xml,” width=”798″>

You can instruct zip to remove original files after compression. To achieve it, you need to add the -m flag, as shown in this example.

After zipping files linuxhint1, linuxhint2 and linuxhint3, the files will be removed and remain only in their compressed format called zippedfiles5.zip.

By adding the -m option, original files will be removed after being zipped.

zip -m zippedfile5.zip linuxhint1 linuxhint2 linuxhint3

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/7.png" data-lazy- height="263" src="data:image/svg xml,” width=”1120″>

Unzipping or Extracting Files using Unzip:

Unzipping files is even easier than compressing them. To extract a zipped file, run the unzip command followed by the file you want to extract, as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/8.png" data-lazy- height="403" src="data:image/svg xml,” width=”798″>

If you type the filename without extension, unzip will detect it and extract the file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/9.png" data-lazy- height="403" src="data:image/svg xml,” width=”798″>

You also can use the wildcard to extract multiple zipped files. The difference is in some cases like in the current example, you need to add a backslash before the wildcard.

In the following example, there are files named linuxhint1, linuxhint2, linuxhint3, linux-hint, and linux-hint2. I will extract only the first 3 files by applying the backslash and wildcard in the same way I did when zipping to extract all similarly named files whose last part of their name or extension is different.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/10.png" data-lazy- height="487" src="data:image/svg xml,” width=”854″>

To extract all files within the current directory, you also need to implement the backslash before the wildcard, as shown in the following screenshot.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/11.png" data-lazy- height="543" src="data:image/svg xml,” width=”854″>

In this example, there is a file called allzipped.zip which contains files named linuxhint1, linuxhint2, linuxhint3, linux-hint, and linux-hint2. The example shows how to extract files selectively, omitting specific files.

The command below instructs unzip to extract all files except linux-hint and linux-hint2.

unzip allzipped.zip -x linux-hint linux-hint2

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/12.png" data-lazy- height="319" src="data:image/svg xml,” width=”1078″>

Contrary to the previous example, if you only want to extract a few files from a zipped file, you can specify them after the zipped file, and the rest of the files won’t be extracted.

The example below instructs unzip to extract only files linuxhint1 and linuxhint2 from allzipped.zip.

unzip allzipped.zip linuxhint1 linuxhint2

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/13.png" data-lazy- height="207" src="data:image/svg xml,” width=”1008″>

You also can specify a directory for files to be extracted using the -d flag, followed by the path. In the following example the -d flag is used to store unzipped files linuxhint, linuxhint1, linuxhint2, and linuxhint3 within the linuxhint directory located in the home directory.

unzip zippedfile2 -d ~/linuxhint

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/14.png" data-lazy- height="291" src="data:image/svg xml,” width=”896″>

When extracting files to a location where the same files are stored, unzip will request manual overwriting confirmation for each repeated file.

To unzip files overwriting existing files you need to implement the -o flag, as shown in the following screenshot.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/15.png" data-lazy- height="319" src="data:image/svg xml,” width=”728″>

Protecting Zipped Files with a Password:

A nice zip function is the ability to protect files with a password. To achieve this, you need to implement the -e option followed by the zip file name you want to create and the file you want to zip. A prompt will ask you to type and verify the password, as shown in the example below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/16.png" data-lazy- height="179" src="data:image/svg xml,” width=”882″>

To unzip a protected zip file just run unzip as normal, and type the password when requested, as shown in the following image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/17.png" data-lazy- height="235" src="data:image/svg xml,” width=”882″>

Other Zip and Unzip Operations:

One of the .zip format advantages over other compression methods is that zip allows you to see the content of zipped files without the need for extraction. This is a very interesting feature that is easy to practice by implementing the -l flag.

The example below shows the use of the -l flag to show the content of the file named allzipped.zip.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/18.png" data-lazy- height="431" src="data:image/svg xml,” width=”868″>

You can get additional information without extracting by replacing the -l flag with -Z. This option shown in the example below will print information on file permissions, size, and creation time.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/19.png" data-lazy- height="375" src="data:image/svg xml,” width=”1008″>

Another way to show the content of a zipped file is the zipinfo command. The following screenshot shows zipinfo displaying the content of the linuxhint1.zip file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/20.png" data-lazy- height="347" src="data:image/svg xml,” width=”1008″>

Zip also allows you to add files to an already zipped file. To achieve this, you need to add the -u flag followed by the zipped file and the file you want to add to the zipped file, as shown in the example below.

In the screenshot below the command zip and the -u flag are used to add file linuxhint2 to the zipped file linuxhint1.zip.

zip -u linuxhint1.zip linuxhint2

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/21.png" data-lazy- height="179" src="data:image/svg xml,” width=”896″>

Contrary to the previous example, you also can remove specific content from a zipped file.

To achieve this, you need to implement the -d flag followed by the zipped file name and the file you want to delete from it (the -d flag is used for this purpose with the zip command and to define directories with the unzip command).

The example below shows how to remove the file linuxhint2 from the file linuxhint1.zip using the zip command with the -d flag.

zip -d linuxhint1.zip linuxhint2

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/22.png" data-lazy- height="207" src="data:image/svg xml,” width=”1008″>

Conclusion:

As you can see zip and unzip are complete and very easy-to-use tools to deal with file compression. The zip format is a multiplatform extension and learning how to use it is advantageous for any Linux user. As said previously, it also has functions that are not present in other compression methods, such as showing contained files and directories without extraction, omitting specific files when extracting, or adding individual files to an already zipped file.

I hope this tutorial about zip and unzip files on Linux was useful. Keep following Linux Hint for more Linux tips and tutorials.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/linuxinstitute_icono-150×150.png" height="112" src="data:image/svg xml,” width=”112″>

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.