How to Use ZIP and UNZIP Command to Create, Extract ZIP File in Linux

Learn some common way to create and extract a zip file in Linux

Definitely, you have used 7zip or Winrar to create a zip file in Windows, but here in Linux, you can use the zip command to create a compressed archive of files and directories. 

Although tar (tape archive) is much more common to see in Linux, zip is still a useful tool for compressing files and sharing them with others who may not be familiar with tar or sending files to Windows users.

Additionally, zip files can be compressed with encryption, so you can ensure that the contents of the file remain secure and protected from unauthorized access. 

More importantly, zip files can be easily shared and transferred as collections of files all at once without the need to individually send each file, saving time and effort in the process.

So, let’s see how you can use the zip command to create a zip file, add a new file to an existing zip file, list zip file content without extracting it, delete the contents of zip, and encrypt zip with a password for added security. 

Syntax and options of zip command

On most Linux distributions, you will find the presence of zip command, so you don’t need to worry about that. You can simply follow the basic syntax of zip command:

$ zip <options> <name-of-zip-file> <list-of-file-to-archive>

Here are the some option that you can use with zip command

  • -d or --delete: It is use to delete file from the zip files
  • -e or --encrypt: For enabling password to zip file
  • -f or --freshen: Without extracting file you can update and add new file to zip file
  • -e or --exclude: When you don’t want to include specific file or directories to add the compress
  • -sf or --show-files: List a content of zip file

Now let me get into more detail about the usage of the zip command and the available options.

Usage of zip Command

To start with the usage, it’s better to create a new directory and add some samples that will be used to execute the task, like creating zip files, listing zip files, removing content from them, and so on.

Now let’s create a directory called “test-arena” where I’ll add five different files using brace expansion method.

$ mkdir test-arena && cd test-arena
$ touch sample{1..5}.txt

Once you are done with the above, let’s start the usage of the zip command by showing you how to create a zip file in Linux.

Create Zip File in Linux

When you want to create a zip file in Linux, you can use the following command syntax where you need to first specify the name of the zip file, and then specify the source of the file that you want to zip.

$ zip /list-of-files-to-zip/

For instance, if I want to make an archive of all the *.txt files available in the test-arena directory, then I’ll use the following statement to create the archive:

$ zip *.txt

Once you invoke the above command it will print the file name that has been added to zip file along with percentage of deflation(reduce size using deflate compression algorithm).

Create zip file in Linux
Create a zip file in Linux

Isn’t it that simple to create a zip file?

Add Directory or Folder Content to ZIP

The problem with the above command is that it doesn’t include the file of a directory while compressing the file, and because of that, you will end up with empty directories, which I don’t think you will like to do.

To avoid this, you can use the -r or -R option to recursively copy the content of a directory and subdirectories.

For this purpose, I have included a couple of directories and subdirectories in test-arena to show you the result when you invoke the below command:

$ zip -r *

The above command will even add an empty file. If you don’t want that to include it, use -R

$ zip -R *

If you just want to compress a specific directory, then just specify the directory name.

$ zip -r dir-1/ dir-2/

Choose Compression Method

While creating a zip file, you have seen that the zip command uses the deflate algorithm to compress the file without losing much information, but if you want a more robust option to compress the file, you can use other compression algorithms like bzip2.

To use bzip2 compression algorithms, use -Z and specify the algorithm name like shown below:

$ zip -Z bzip2 *

Result of the above command:

Choose Compression method
Choose Compression method

Exclude Files

Another interesting option is --exclude, which allows you to exclude a directory or file from the pattern that you should not include while creating a zip.

Like I have ~/test-areana/dir-1 which includes a couple of files, along with the other directory where more files are present, but I just want it to ignore the ~/test-areana/dir-1/.

$ zip -r * --exclude "dir-1*"       

The output of the above command:

Exclude specific directory for compressing
Exclude specific directory for compressing

List Zip File in Linux

Now the next question that arises in my mind  is there any possibility to list out the content of a zip file without extracting the zip file? Of course, you can do this by using the -sf or --show-files options, as shown below.

$ zip -sf

Result of the above command:

List zip file
List zip file

Update Zip File in Linux

The zip command doesn’t restrict you from adding new files or directories to the already existing zip file, so you can easily update and add new content to the zip file without having to create a new one from scratch.

Here you can modify the zip file in two ways: one will update the content of the zip file with the new data, and the other will also update the previous content of the zip file and also add a new file.

To update and add new files, you can use the -u or --update options, as shown below:

$ zip -u newfile
$ zip -u *

Result of the above command:

Update zip file
Update zip file

If you just want to update the content of the zip file with new data, use the -f or –freshen option, as shown below:

$ zip -f

Delete Files from Zip

After updating the new file to a zip file, you have to remove the file from the directory, but when you run zip -sf, it shows the file is present. 

I think if the file is not required any more, you should remove it from zip to avoid further confusion.

Don’t worry, you can simply remove or delete individual components from the zip file using the -d or --delete options, as shown below:

$ zip -d sample5.txt

If the file exist in the sub-directory, then you should specificy the path where the file is located in the zip file. For example here “sample5.txt” exist in “” so you just need to use dir1/foo.txt after specifiying the zip name.

$ zip -d new-dir/sample5.txt 

Result of the above command:

Delete file from zip file
Delete file from zip

Encrypt Zip File in Linux

The zip command also allows you to encrypt a zip file with a password so that the file can be accessed by only authorized users.

To encrypt a zip file, you can use the -e or --encrypt option to set the password required during the extraction of the zip file.

$ zip -e sample1.txt sample2.txt sample3.txt

While testing, I found it was not appending a password to an already created zip file, so you need to start from scratch to create a zip file with password enabled.

$ zip -e *

I think I should end here and show you how to unzip.

Unzip File in Linux

Above, you learned how to create a zip file with different options, but I haven’t yet shown you how to unzip a file in Linux, so let me show you how to use the unzip command in Linux with examples.

First, let’s take the example of extracting a zip file into the current directory.

From the above steps, we have already created some file, let’s extract it by executing the following command:

$ unzip 

Result of the above command:

Unzip zip file in Linux
Unzip file

If you want to extract a file to a specific directory, then use the -d option to specify the path of the directory where you want to extract the file.

$ unzip -d ~/new-dir

If you have encrypted the zip file with a password, then it will prompt you to enter the password, so please enter the password to extract the file.

Wrap up

That’s all for this article, where you learn how to use the zip and unzip commands in Linux to create and extract zip files through the command line.

I hope you are now able to compress files easily with the zip command.

If you find anything that needs some clarification, then let me know in the comment section.

Leave a Reply