zip Command in Linux


Zipping is the phenomenon of packaging single or multiple files into a compressed format. Apart from creating the zip only, it also assists in managing the zip files it saves space, enhances the efficiency, and enables the smooth performance/transfer.

Ubuntu, like other linux distributions, offers strong command-line support to manage routine operations. Ubuntu has the zip command to carry out zipping from the terminal. Keeping this importance in view, we will elaborate on the zip command.

Outline:

zip Command in Linux

The zip command zips the content of the directory or multiple files. It not only zips the content but also allows you to encrypt the files, extract or add specific files from the whole zip, and so on. Before we get into the use cases, let’s have a look at the syntax of the zip command:

Syntax

zip [Options] [Zip to be created] [files]

This is the general syntax of the zip command where you need to put the options, give the path of the zip file to be created, and the files to be zipped. You can find the details of the options supported and the overall functionality of the zip command as:

zip

Let’s understand it by working through the following use cases:

Example 1: Zipping Single/Multiple Files

The primary purpose of the zip command is to create a zip file of a single or multiple files. For instance, the command below zips the two files “test.txt” and the “exer.txt”:

zip linuxgenie.zip test.txt exer.txt

You can create the zip of the one file in the same way. The output of the screenshot is as follows:

Example 2: Move/Delete Files While Zipping Them

By default, the zip command copies the files and creates a new zip file. However, by using zip command, you can move the files into the zip file. For that, use the “-m” option which moves the files into a zip, as shown below

zip -m lgenie.zip exer.txt test.sh

After this command, the “exer.txt” and the “test.sh” will be removed from their original locations to the zip file:

Example 3: Zipping a Directory/Content of the Directory

The zip command allows you to zip the directory recursively. Like, all the content of the directory and the sub-directory is zipped in a single file. Moreover, you can exclude a few files from the directory. Those files will not be zipped. Let’s practice both scenarios:

  • Example 3.1: Zipping the Whole Directory Recursively

Primarily, if you use the “-r” flag and provide the path of the directory. The whole directory is zipped recursively. For instance, the below command zips the directory “test” into a zip file “linuxtest.zip”:

zip -r linuxtest.zip test

The “test” is the directory that is zipped recursively and the zip file is named “linuxtest.zip”:

  • Example 3.2: Zipping the Directory By Excluding a Few Content

You can exclude (using the “-x” flag) a few files/data from the directory while zipping it (the directory). The command below zips the directory “test” (into a zip file “linux.zip”) but excludes its sub-directory “tt”:

zip -r linux.zip test -x "test/tt/*"

The ls command shows that one “txt” file and one sub-directory is inside the main directory that will be zipped:

The output of the command shows that only the text file is zipped while the “tt” directory and its content are excluded.

Example 4: Modify the Existing ZIP

You can add or remove extra files/data to an existing zip file. Similarly, you can update the content of the existing file(s) inside the zip file. Let’s understand this with the examples:

  • Example 4.1: Add Extra Files/Data

If you want to zip more files/data into an existing zip, you need to use the same syntax as creating a zip file. For example, the command below adds two files to an already created zip, i.e., code.zip:

zip code.zip ded.txt tax.txt

Note: If the “code.zip” is not found, a new zip file “code.zip” will be created with the “ded.txt” and “tax.txt” files in it.

  • Example 4.2: Update the Existing File in the Zip

If you want to update the already zipped file, you can do it using the “-u” flag. Like, the below command updates the file “tax.txt” in the zip file named “code.zip”:

zip -u code.zip tax.txt

  • Example 4.3: Remove a File From a ZIP File

Like addition, the zip command can be used to delete the specific file/data from that zip. For that, the “-d” option is used:

Zip -d code.zip ded.txt

Example 5: Creating a Password Protected zip

One of the main functionality of the zip command is that you can protect the zip with a password. For that, you need to use the “-e” option, as we did here:

zip -e genie.zip test.txt tax.txt

While executing, you will be asked to set the password and confirm it.

Now, whenever a person tries to extract it or reuse it, a password will be required. Here’s the experiment that we performed:

Your zip is password-protected now. Now, what happens if the zip is protected? Let’s try to unzip it and check what happens:

unzip genie.zip

While unzipping, you could see the password prompt. We will provide the password that we set up for “genie.zip”:

Upon successful authentication, the file will be unzipped.

More Use Cases

Until now, we have demonstrated all the possible use cases (most used) of the zip command. For more grip on the zip command, you can try out different options. To get the details/list of the supported options, go through the manual page of it:

zip --help

That’s all about the zip command in Linux.

Bottom Line

The zip command zips the files and directories with multiple options. Zipping keeps your files protected from security breaches and also optimizes the memory while combining/zipping them. To secure it more, you can use the password-protected zip functionality.

All in all, this post has demonstrated the syntax and primary to advanced use cases of the zip command in Linux. We have used Ubuntu to practically demonstrate the use cases of the zip command. However, the zip command has the same functionality (syntax and use cases) across all the Linux distributions.

Print Friendly, PDF & Email
Categories