ZIP files are a universal archive commonly used on Windows, macOS, and even Linux systems. You can create a zip archive or unzip files from one with some common Linux terminal commands.
The ZIP Compressed Archive File Format
Thanks to the dominance of the ZIP format in the Windows realm, ZIP files are probably the most common form of compressed archive in the world.
While .tar.gz and tar.bz2 files are common on Linux, Windows users will probably send you an archive in ZIP format. And, if you want to archive some files and send them to a Windows user, the ZIP format will be the easiest, most compatible solution for everyone.
zip, unzip, and Other Utilities
You may already know that Linux and Unix-like operating systems such as macOS have tools to allow you to create ZIP files and extract files from them, called
unzip. But there’s a whole family of related utilities such as
zipsplit , and
We checked some Linux distributions to see whether they included these utilities in the standard installation. All of the utilities were present in Ubuntu 19.04, 18.10, and 18.04. They were also present in Manjaro 18.04. Fedora 29 included
unzip, but none of the other utilities and that was also the case for CentOS.
To install the missing elements on Fedora 29, use the following command:
sudo dnf install perl-IO-Compress
To install the missing elements on CentOS 7, use the following command:
sudo yum install perl-IO-Compress
If any of the zip utilities are missing from a Linux distribution that wasn’t mentioned above, use that Linux distribution’s package management tool to install the required package.
How to Create a ZIP File with the zip Command
To create a ZIP file, you need to tell
zip the name of the archive file and which files to include in it. You don’t need to add the “.zip” extension to the archive name, but it does no harm if you do.
To create a file called
source_code.zip containing all the C source code files and header files in the current directory, you would use this command:
zip source_code *.c *.h
Each file is listed as it’s added. The name of the file and the amount of compression that was achieved on that file is shown.
If you look at the new ZIP archive, you can see that the “.zip” file extension has been added automatically by
ls -l source_code.zip
If you do not want to see the output from
zip as the ZIP file is created, use the
-q (quiet) option.
zip -q source_code *.c *.h
Including Directories in ZIP Files
To include sub-directories in the ZIP file, use the
-r (recursive) option and include the name of the sub-directory on the command line. To create a ZIP file as before and also include the archive sub-directory, use this command.
zip -r -q source_code archive/ *.c *.h
To be considerate to the person who will be extracting the files from the ZIP file you’re creating, it is often polite to create ZIP files with the files inside it contained in a directory. When the person who receives the ZIP file extracts it, all of the files are placed neatly within a directory on their computer.
In the following command, we’re going to archive the
work directory and all sub-directories. Note that this command is being issued from the parent directory of the
zip -r -q source_code work/
Setting the Level of Compression
You can set how much compression is applied to the files as they are added to the ZIP archive. The range is from 0 to 9, with 0 being no compression at all. The higher the compression, the longer it takes to create the ZIP file. For modestly sized ZIP files, the time difference isn’t a significant penalty. But then, for modestly sized ZIP files, the default compression (level 6) is probably good enough anyway.
zip to use a specific level of compression, pass the number as an option on the command line, with a “-“, like this:
zip -0 -r -q source_code work/
The default compression level is 6. There is no need to provide the
-6 option, but it will do no harm if you do.
zip -r -q source_code work/
The maximum compression level is level 9.
zip -9 -r -q source_code work/
With the selection of files and directories being 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 level of compression (level 9) is only 4K.
That might not seem much, but for archives containing hundreds or even thousands of files, the small amount of extra compression per file would add up to a worthwhile space saving.
Adding Passwords to ZIP Files
Adding passwords to ZIP files is easy. Use the
-e (encrypt) option and you’ll be prompted to enter your password and to re-enter it for verification.
zip -e -r -q source_code work/
How to Unzip a ZIP File With the unzip Command
To extract the files from a ZIP file, use the unzip command, and provide the name of the ZIP file. Note that you do need to provide the “.zip” extension.
As the files are extracted they are listed to the terminal window.
ZIP files don’t carry details of file ownership. All of the files that are extracted have the owner set to the user who is extracting them.
unzip has a
-q (quiet) option, so that you do not need to see the file listing as the files are extracted.
unzip -q source_code.zip
Extracting Files to a Target Directory
To have the files extracted in a specific directory, use the
-d (directory) option, and provide the path to the directory you wish the archive to be extracted into.
unzip -q source_code.zip -d ./development
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 extract the files.
unzip -q source_code.zip
If you don’t care about your password being seen by others—nor about it being stored in your command history—you can provide the password on the command line with the
-P (password) option. (You must use a capital “P.”)
unzip -P fifty.treacle.cutlass -q source_code.zip
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 of the files apart from those ending in a “.h” extension.
unzip -q source_code.zip -x *.h
Suppose you have extracted an archive but you have deleted a few of the extracted files by mistake.
A quick fix for that would be to extract the files once again. But if you try to extract the ZIP file in the same directory as before,
unzip will prompt you for a decision regarding overwriting the files. It will expect one of the following responses.
Apart from the
r (rename) response, these responses are case sensitive.
- y: Yes, overwrite this file
- n: No, don’t overwrite this file
- A: All, overwrite all of the files
- N: None, overwrite none of the files
- r: Rename, extract this file but give it a new name. You will be prompted for a new name.
unzip to overwrite any existing files use the
-o (overwrite) option.
unzip -o -q source_code.zip
The most efficient way to replace the missing files would be to have
unzip only extract any files in the archive that are not in the target directory. To do this, use the
-n (never overwrite) option.
unzip -n source_code.zip
Looking Inside a ZIP File
It is often useful and instructive to see a list of the files inside a ZIP file before you extract it. You can do this with the
-l (list archive) option. It is piped through
less to make the output manageable.
unzip -l source_code.zip | less
The output shows the directories and files within the ZIP file, their length and the time and date they were added to the archive. Press “q” to quit from
There are other ways to peek inside a ZIP file which give different types of information, as we shall see.
Add a Password With the zipcloak Command
If you’ve created a ZIP file but forgot to add a password, what can you do? You can quickly add a password to ZIP file using the
zipcloak command. Pass the name of the ZIP file on the command line. You will be prompted for a password. You need to verify the password by entering it a second time.
View File Details With the zipdetails Command
zipdetails command will show you a lot of information regarding the ZIP file. The only sensible way to handle the amount of output this command can give is to pipe it through
zipdetails source_code.zip | less
Note that the information will include filenames even if the ZIP file is password protected. This type of information is stored within the ZIP file as meta-data and is not part of the encrypted data.
Search Inside the File With the zipgrep Command
zipgrep command allows you to search within the files in a ZIP file. In the following example, we want to know which files within the ZIP file have the text “keyval.h” in them.
zipgrep keyval.h source_code.zip
We can see that the files
getval.c contain the string “keyval.h”.We can also see that there are two copies of each of these files in different directories in the ZIP file.
View Information With the zipinfo Command
zipinfo command gives you yet another way to look inside a ZIP file. As before, we pipe the output through
zipinfo source_code.zip | less
From left to right the output shows:
- The file permissions
- The version of the tool used to create the ZIP file
- The original file size
- A file descriptor (described below)
- The method of compression (deflation, in this case)
- The data and time stamp
- The name of the file and any directory
The file descriptor is made up of two characters. The first character will be a “t” or a “b” to indicate a text or binary file. If it is a capital letter the file is encrypted. The second character may be one of four characters. This character represents what type of meta-data is included for this file: none, an extended local header, an “extra field”, or both.
- -: If neither exists, the character will be a hyphen
- l: if there is an extended local header but no extra field
- x: if there is no extended local header but there is an extra field
- X: if there is an extended local header and there is an extra field
Split the File With the zipsplit Command
If you need to send the ZIP file to someone else but there are size restrictions or problems with the transmission of the file, you can use the
zipsplit command to split the original ZIP file into a set of smaller ZIP files.
-n (size) option allows you to set a maximum size for each of the new ZIP files. In this example, we’re splitting the
source_code.zip file. We don’t want any of the new ZIP files to be bigger than 100 KB (102400 bytes).
zipsplit -n 102400 source_code.zip
The size that you choose cannot be smaller than the size of any of the files in the ZIP file.
Using these commands, you can create your own ZIP files, unzip ZIP files you receive, and perform various other operations on them without ever leaving the Linux terminal.
- › How to Turn off a Chromebook Screen When Connected to an External Display
- › How to Use Your iPhone or iPad as an Apple TV Remote
- › How to Generate Two-Factor Authentication Codes in 1Password
- › 3 Ways to “Bookmark” a Folder in Finder on Mac
- › How to Automatically Back up Windows Folders to OneDrive