How to Zip or Unzip Files From the Linux Terminal

Linux shell prompt

ZIP files are a universal archive commonly used on Windows, MacOS and even Linux systems. You can create a zip archive or unzip files using common Linux terminal commands.

ZIP compressed archive file format

Due to the predominance of the ZIP format in the Windows world, ZIP files are probably the most common form of compressed archive in the world.

While .tar.gz and tar.bz2 files are common in Linux, Windows users will probably send you an archive in ZIP format. In addition, if you want to archive some files and send them to a Windows user, the ZIP format will be the simplest and most compatible solution for everyone.

RELATED: How to extract files from a .tar.gz or .tar.bz2 file on Linux

zip, unzip and other utilities

You may already know that Linux and Unix-like Operating systems such as macOS include tools to create ZIP files and extract files, called zip and unzip. But there is a whole family of related utilities like zipcloak, zipdetails, zipsplit and zipinfo.

We checked some Linux distributions to see if they included these utilities in the standard installation. All the utilities were present in Ubuntu 19.04, 18.10 and 18.04. They were also present in Manjaro on 18.04. Fedora 29 included zip and unzip, but no other utility, which was also the case for CentOS.

To install the missing items on Fedora 29, use the following command:

sudo dnf installs perl-IO-Compress

zip installation command for Fedora

To install the missing items on CentOS 7, use the following command:

sudo yum installs perl-IO-Compress

Installation command zip in centos

If one of the zip utilities is missing in a Linux distribution not mentioned above, use the package management tool of this Linux distribution to install the required package.

How to create a ZIP file with the zip command

To create a ZIP file, you must specify the zip file name and the files to include. It is not necessary to add the extension ".zip" to the name of the archive, but it does not hurt if you do it.

To create a file called containing all the C source code files and the current directory header files, use the following command:

zip source_code * .c * .h

zip command in a terminal window

Each file is listed as it is added. The file name and the amount of compression obtained on this file are displayed.

output of the zip command in a terminal window

If you look at the new ZIP archive, you can see that the ".zip" file extension has been added automatically by zip.

ls -l

ls on zip archive in a terminal window

If you do not want to see the output of the zip file when creating the ZIP file, use the -q (quiet) option.

zip -q source_code * .c * .h

-q quiet option in a terminal window

Including directories in ZIP files

To include subdirectories in the ZIP file, use the -r (recursive) option and include the subdirectory name on the command line. To create a ZIP file as before and also include the archive subdirectory, use this command.

zip -r -q archive_code_source / * .c * .h

-r recursive option in a terminal window

To be attentive to the person who will extract the files from the ZIP file that you create, it is often polite to create ZIP files with the files it contains, contained in a directory. When the person receiving the ZIP file extracts it, all files are carefully placed in a directory on their computer.

In the following command, we will archive the working directory and all subdirectories. Note that this command is issued from the parent directory of the working folder.

zip -r -q work_source_code /

compress the working directory in a terminal window

Adjusting the compression level

You can define the degree of compression applied to files as they are added to the ZIP archive. The range is from 0 to 9, with 0 being no compression. The higher the compression, the longer the creation of the ZIP file. For modest sized ZIP files, the time difference is not a significant penalty. However, for small ZIP files, the default compression (level 6) is probably sufficient.

For zip to use a specific compression level, pass the optional number on the command line, with a "-", like this:

zip -0 -r -q source_code work /

-0 compression option in a terminal window

The default compression level is 6. There is no need to provide the -6 option, but it will not hurt if you do it.

zip -r -q work_source_code /

level 6 compression in a terminal window

The maximum compression level is level 9.

zip -9 -r -q source_code work /

-9 compression in a terminal window

With the selection of files and directories archived here, the difference between no compression (level 0) and the default compression (level 6) is 400K. The difference between the default compression and the highest compression level (level 9) is only 4 KB.

This may not seem like much, but for archives containing hundreds or even thousands of files, the small amount of additional compression per file would help to save valuable space.

Adding passwords to ZIP files

Adding passwords to ZIP files is easy. Use the -e (encryption) option. You will be prompted to enter your password and enter it again for verification.

zip -e -r -r -q source_code work /

Adding passwords to zip files in a terminal window

How to uncompress a ZIP file with the unzip command

To extract the files from a ZIP file, use the unzip command and specify the name of the ZIP file. Note that you must provide the ".zip" extension.


the decompression command in a terminal window

As the files are extracted, they are listed in the terminal window.

output to decompress in a terminal window

ZIP files do not contain the details of the file property. The owner of all extracted files has been set to the user who extracts them.

Just like zip, unzip has a -q (silent) option, so you do not need to see the list of files because the files are extracted.

unzip -q

Uncompress the -q option in a terminal window

Extracting files to a target directory

To have the files extracted into a specific directory, use the -d (directory) option and specify the path to the directory where you want to extract the archive.

unzip -q -d ./development

unzip into a target directory in a terminal window

Extract password protected ZIP files

If a ZIP file has been created with a password, unzip will ask you for the password. If you do not provide the correct password, unzip will not be able to extract the files.

unzip -q

the decompression command with password in a terminal window

If you do not care about your password seen by others – or stored in your order history – you can provide it on the command line with the -P option (password). (You must use a capital "P.")

uncompress -P fifty.treacle.cutlass -q

Decompression with the option password in a terminal window

Exclusion files

If you do not want to extract a particular file or group of files, use the -x (exclude) option. In this example, we want to extract all files except those ending with an ".h" extension.

unzip -q -x * .h

Uncompress the exclusion option in a terminal window

Overwrite files

Suppose you have extracted an archive but mistakenly deleted some of the extracted files.

A quick fix would be to extract the files one more time. But if you try to extract the ZIP file in the same directory as before, unzip will ask you to make a decision regarding replacing the files. He will expect one of the following answers.

In addition to the r (rename) response, these responses are case-sensitive.

there: Yes, overwrite this file
not: No, do not overwrite this file
A: All, overwrite all files
NOT: None, overwrite none of the files
r: Rename, extract this file but give it a new name. A new name will be required.

to overwrite the prompts in a terminal window

To force decompression to overwrite all existing files, use the -o (overwrite) option.

unzip -o -q

option to overwrite in a terminal window

The most effective way to replace missing files would be to uncompress only those archive files that are not in the target directory. To do this, use the -n option (never overwrite).

unzip -n

Unzip with the option -n never overwrite in a terminal window

Look inside a ZIP file

It is often useful and informative to see the list of files contained in a ZIP file before you extract it. You can do this with the -l option (list archive). It is ducted through less to make the output manageable.

uncompress -l | less

Uncompress the listing option in a terminal window

The output displays the directories and files of the ZIP file, their length, as well as the time and date of their addition to the archive. Press "q" to quit less.

list the output of the archive in a terminal window

As we will see, there are other ways to take a look in a ZIP file.

Add a password with the zipcloak command

If you have created a ZIP file but forgot to add a password, what can you do? You can quickly add a password to the ZIP file using the zipcloak command. Pass the name of the ZIP file on the command line. You will be prompted to enter a password. You must verify the password by typing it a second time.


zipcloak in a terminal window

View file details with the zipdetails command

The zipdetails command will show you a lot of information about the ZIP file. The only sensible way to handle the amount of output that this command can give is to direct it less.

zipdetails | less

the zipdetails command in a terminal window

Note that the information will include filenames even if the ZIP file is password protected. This type of information is stored in the ZIP file as metadata and is not part of the encrypted data.

zipdetails output in a terminal window

Search the file with the zipgrep command

The zipgrep command allows you to search files in a ZIP file. In the following example, we want to know which files in the ZIP file contain the text "keyval.h".

zipgrep keyval.h

the zipgrep command in a terminal window

We can see that the files slang.c and getval.c contain the string "keyval.h". We can also see that there are two copies of each of these files in different directories of the ZIP file.

View information with the zipinfo command

The zipinfo command gives you yet another way to look inside a ZIP file. As before, we transmit the output by less.

zipinfo | less

Zipinfo command in a terminal window

From left to right, the exit shows:

File permissions
The version of the tool used to create the ZIP file
The size of the original file
A file descriptor (described below)
The compression method (deflation, in this case)
Data and timestamp
The name of the file and any directory

The file descriptor is composed of two characters. The first character will be a "t" or a "b" to indicate a text or binary file. If it is an uppercase letter, the file is encrypted. The second character can be one of four characters. This character represents the type of metadata included for this file: none, an extended local header, an "extra field", or both.

-: If none exists, the character will be a hyphen
l: if there is an extended local header but no additional field
X: there is no local header extended but one additional field
X: if there is an extended local header and an additional field

Zipinfo output in a terminal window

Split the file with the zipsplit command

If you need to send the ZIP file to someone else in case of size restrictions or transmission problems, you can use the zipsplit command to split the ZIP file. originated in a set of smaller ZIP files.

The -n (size) option allows you to set a maximum size for each new ZIP file. In this example, we split the file. We do not want new ZIP files to exceed 100 KB (102400 bytes).

zipsplit -n 102400

the zipsplit command in a terminal window

The size you choose can not be smaller than the size of any of the ZIP file files.

Using these commands, you can create your own ZIP files, unzip the ZIP files you receive, and perform various other operations without even leaving the Linux terminal.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.