Working with Zip Command in Linux


Overview

The zip command is one of the most useful tools available on any operating system. It allows you to compress multiple files into a single compressed archive file. You can then decompress that archive file using the unzip command. This tutorial will show you how to use the zip command to create and extract archives.

In this tutorial, we’ll be looking at the zip command-line tool in Linux. We will learn how to use it for creating and extracting archives. The zip command is a part of the GNU core utils package. It can be used to create and extract files from an archive file.

Basic Usage

One of the most basic uses for the `z` (or `x`) command is to compress a file into an archive. For example, we could compress two text documents into an archive called important-backup.tar.gz by typing −

$ zip important-backup.zip credentials.txt statement.txt
adding: credentials.txt (stored 0%)
adding: statement.txt (stored 0%)

Listing Archive Contents Without Unarchiving

We sometimes just need to look at the contents of an archive without extracting them. To achieve this, use the zipinfo command and the unarchive command, both of which are part of the zip package.

If we want to see the contents of an archive file called "archive.zip", we can use the command line tool zipinfo to do so −

$ zipinfo important-backup.zip
Archive: important-backup.zip
Zip file size: 347 bytes, number of entries: 2
-rw-r--r-- 3.0 unx 8 tx stor 21-May-03 11:22 credentials.txt
-rw-r--r-- 3.0 unx 5 tx stor 21-May-03 11:22 statement.txt
2 files, 13 bytes uncompressed, 13 bytes compressed: 0.0%

We can use the unzip command to list the contents of an archived file directly.

$ unzip -l important-backup.zip
Archive: important-backup.zip
Length Date Time Name
--------- ---------- ----- ----
8 2021-05-03 11:22 credentials.txt
5 2021-05-03 11:22 statement.txt
--------- -------
13 2 files

Using the -l option, the tar program doesn't actually extract the archive but just displays its contents.

Deleting Files From Archive Without Unzipping

If you want to remove some files from an archived backup without extracting them, use the -d option for the zip command. For example, if you want to remove the statement.txt file in the important-backup folder, run the following command −

$ zip -d important-backup.zip statement.txt
deleting: statement.txt

Let's see what's inside the archive important-backups.zip file after the commands return.

$ zipinfo important-backup.zip
Archive: important-backup.zip
Zip file size: 188 bytes, number of entries: 1
-rw-r--r-- 3.0 unx 8 tx stor 21-May-03 11:22 credentials.txt
1 file, 8 bytes uncompressed, 8 bytes compressed: 0.0%

We can see that statement.txt has been deleted from the zip file without having to first unzip its contents.

Encrypting an Archive

The zip command provides the -e flag to encrypt archives, asking for a password that serves as an encryption key for the resulting encrypted archive.

Let’s create and encrypt an archive file encrypted-backup.zip −

$ zip -e encrypted-backup.zip bank-accounts.txt credentials.txt
Enter password:
Verify password:
adding: bank-accounts.txt (stored 0%)
adding: credentials.txt (stored 0%)

By invoking the -e flag, this command will ask us for a password which will be used to encrypt the data.

If we now attempt to unzip the archive, it’ll ask for a password to unarchive it −

$ unzip encrypted-backup.zip
Archive: encrypted-backup.zip
[encrypted-backup.zip] bank-accounts.txt password:

However, we can still list the content of an encrypted archive using tools like zipinfo and unzip without knowing the password −

$ zipinfo encrypted-backup.zip
Archive: encrypted-backup.zip
Length Date Time Name
--------- ---------- ----- ----
17 2021-05-03 11:49 bank-accounts.txt
8 2021-05-03 11:22 credentials.txt
--------- -------
25 2 files

As we can see, it doesn’t prompt us for the password when we attempt to read its content listing.

Traversing Subdirectories Recursively

Usually, the zip command will not create archives of a directory and its contents. But, when the -r flag is added, zip collects files from subdirectories to form a single archive.

For example, let’s say we have a directory prod-secret that contains some secret files −

$ ls prod-secret
kafka-passwd.secret mongo-passwd.secret mysql-passwd.secret

To create an archive of the entire prod-secret directory, we can run the zip command with flag -r −

$ zip -r prod-secret.zip prod-secret
adding: prod-secret/ (stored 0%)
adding: prod-secret/mysql-passwd.secret (stored 0%)
adding: prod-secret/mongo-passwd.secret (stored 0%)
adding: prod-secret/kafka-passwd.secret (stored 0%)

Running the command with option -r ensures the files within the directory are included as well.

However, the zip command will ignore the files within the prod-secret directory without the flag -r

$ zip prod-secret prod-secret
adding: prod-secret/ (stored 0%)

Including and Excluding Files Pattern

We can use the zip file to create filters that will either include or exclude certain files. Furthermore, these filters are identified through the glob pattern. Moreover, these filters can be employed for either creating, deleting or freshening.

Let’s say we are working in the following directory −

$ tree -a
.
|-- .git
| |-- HEAD
| |-- branch
| `-- tag
|-- credentials.txt
|-- customer-details.txt
`-- prod-secret
|-- .git
| |-- HEAD
| `-- branch
|-- kafka-passwd.secret
|-- mongo-passwd.secret
`-- mysql-passwd.secret
 
3 directories, 10 files

When we view the output of the tree command, we can see that our directory follows a graphical structure. Two .git folders appear - one in the current folder and another one in prod-secret.

Including Files and Folders Matching a Pattern

We can use the -i flag for creating an inclusion filter. This means that files should follow the filter pattern in order to be included when using the zip command.

Let’s create an archive only-git.zip that consists of only the .git subfolder −

$ zip -r only-git.zip . -i *.git*
adding: prod-secret/.git/ (stored 0%)
adding: prod-secret/.git/branch (stored 0%)
adding: prod-secret/.git/HEAD (stored 0%)
adding: .git/ (stored 0%)
adding: .git/tag (stored 0%)
adding: .git/branch (stored 0%)
adding: .git/HEAD (stored 0%)

From the output, we can see that the zip command only archives folders and files that match the glob.

Excluding Files and Folders Matching a Pattern

To create an exclusion filter, we can use the flag -x followed by a glob pattern.Zip will ignore files that match the exclusion filter's pattern when it is used.Particularly, we can create an archive that excludes the .git folder using zip -x −

$ zip -r no-git.zip . -x *.git*
adding: prod-secret/ (stored 0%)
adding: prod-secret/mysql-passwd.secret (stored 0%)
adding: prod-secret/mongo-passwd.secret (stored 0%)
adding: prod-secret/kafka-passwd.secret (stored 0%)
adding: customer-details.txt (stored 0%)
adding: credentials.txt (stored 0%)

As we can observe from the output, the command did not add either of the .git folders into the archive no-git.zip.

Conclusion

This tutorial has provided us with a comprehensive overview of the zip command-line tool.

We’ve started with a basic installation guide along with the syntax structure and basic usage. Finally, we covered ways to examine the contents of an archive without unzipping it.

Afterward, we illustrated some selections which give us authority over the already existing archive. Moreover, we illustrated some possibilities for setting up an archive with the use of the recursive flag and filters for what to include and exclude.

Updated on: 03-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements