How To Use ‘zip’: The Complete Linux Command Guide
ZIP files are everywhere, whether downloading software, sharing documents, or organizing your files. It’s one of the most commonly used formats for compressing and packaging files on Linux and other operating systems.
ZIP is a compressed file format, which groups several files and directories into one compressed file reducing its size and making it easy to distribute, as well as containing functions for splitting it into parts or securing it with a password.
In this article, we’ll walk you through how to create and extract ZIP files, how to use various compression levels, and how to protect your ZIP archives with passwords whether you’re compressing files for storage, sharing extensive files, or securing your data.
If you need help with Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your system. Click here to get it for free!
Installing zip
By default, ZIP is not installed on most GNU/Linux operating systems, so you must install it before continuing with the article. Since Ubuntu/Debian is the most common distribution, I will take it as an example for the installation.
You will need two packages: zip to perform the compression process and unzip to decompress the ZIP files. You can install both with these commands:sudo apt install zip
sudo apt install unzip
In other distributions, you can use the commands below to install it, but it depends on the package manager used in every distribution. Here’s a list of the most common distributions and installations:
CentOS/RH Based-Distributionssudo yum install zip
sudo yum install unzip
Arch Based-Distributionssudo pacman -S zip
sudo pacman -S unzip
Fedorasudo dnf install zip
sudo dnf install unzip
Command Syntax
The syntax of ZIP has a similar structure to other file archivers. For example, after the command you have different options plus additional arguments if the option requires it, the name of the compressed, and finally the file or directory to compress it:
zip <option> <zip-name>.zip <files/directories>
In the first section, we have the options argument, which gives you additional features, such as choosing the output directory, the type of compression, or excluding files. Depending on the option, you might have to add an argument along with the option.
The rest of the sections are pretty straightforward. After the option comes the name of the compressed file, which must end in a file type .zip or the file type used. You must then specify the files or directories you want in the compressed file.
Usage Examples
Let’s take a few common examples to see this command in action.
How to compress files into a ZIP Archive
A compressed file contains a set of files or directories grouped into a single ZIP file. When you compress a file or directory, the total size is reduced depending on the compression level.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
To compress one or more files into a ZIP archive, choose the name you want for the archive followed by the file you want to compress. The process may take some time depending on the size of the file:zip <archive-name>.zip <files>
If you have a folder with files and sub-directories, you need to use the -r option, otherwise you will only target the folder without content. You can use the option just like this example:zip -r <archive-name>.zip <directory>
As you can see, you have to consider different options to compress files like the recursive option used. There are more exclude, split, or password options that you will see later in this article.
If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.
How to decompress a ZIP Archive
Decompressing a ZIP archive is the reverse process of the compression, extracting the content of a compressed file, returning it to its original state. You need a compressed file to extract the files inside it.
Once you have the compressed file, you use the unzip command to decompress it. You can indicate the archive and the files will be extracted automatically. If there are duplicate files, the same tool will ask if you want to replace or ignore the files.unzip <archive-name>.zip
You can unzip a single file from the archive to save space and time in cases where you only need one specific file. You must know the archive’s content before doing so (in the next section you will learn how), so you can specify which file to extract:unzip <zip-file>.zip <file-to-decompress>
You can specify which directory you want to save the extracted files by using the -d option followed by the directory. This is useful when you want to save the files on another disk or folder of your choice.unzip -d <directory> <archive-name>.zip
Although the files and directories are compressed using the ZIP tool, you have to use UNZIP, a different tool to extract them.
Viewing the content of a Compressed ZIP
You can view the content of a compressed archive in several ways, one of them, is using VI which is included by default in most GNU/Linux distributions. VI is a text editor but it works to view the contents of a ZIP file that is not split into parts:vi <zip-archive>.zip
If you need a straightforward method, you can use the less command, which will display the contents like the next example. You can exit this menu using the Q key:less <zip-file>.zip
Lastly, you can use unzip for viewing all the content inside a compressed archive, using the option -l followed by the zip file:unzip -l <zip-file>.zip
You can choose any of these methods, there are many more, but these are the most common ones since their tools are already integrated into the distributions or tools that are already used (like unzip).
Main Options
You now know how to use zip and unzip for the most common operations, but there are many subcommands and options that you can use. Let’s summarize them in this section.
ZIP Options
Here’s the list of the most important zip commands and options in a similar format:
Main commands:
- add – Add new files or update existing files in the archive (default behavior).
- update (-u) – Add new files or update existing files only if they have a later modification date.
- freshen (-f) – Update existing files in the archive, but do not add new files.
- filesync (-FS) – Synchronize the archive with the file system, updating files if they have changed and removing files that no longer exist.
- delete (-d) – Remove specified files from the archive.
- copy (-U) – Copy selected entries from the archive to another archive using –out.
Options:
- recurse (-r) – Include files in subdirectories recursively.
- move (-m) – Move files to the archive by deleting originals after archiving.
- quiet (-q) – Suppress output during operation.
- verbose (-v) – Provide detailed output during operation.
- exclude (-x) – Exclude files matching a pattern from the archive.
- include (-i) – Include only files matching a pattern in the archive.
- compression level (-0 to -9) – Set compression level, with -0 for no compression and -9 for maximum compression.
- encrypt (-e) – Encrypt the archive with a password.
- password (-P) – Provide a password for encryption directly (less secure).
- split (-s) – Create a split archive, dividing it into parts of specified size.
Maintenance:
- test (-T) – Verify the integrity of the archive.
- fix (-F) – Attempt to repair a slightly corrupted archive.
- force fix (-FF) – Attempt to salvage as much data as possible from a severely corrupted archive.
- logfile (-lf) – Log the operation details to a specified file.
- display bytes (-db) – Show a running count of bytes processed during the operation.
UNZIP Options
Options:
- extract (-p) – Extract files to standard output (pipe), with no messages.
- list (-l) – List the files in the archive in a short format.
- freshen (-f) – Update only the existing files on the disk, without creating new files.
- test (-t) – Test the integrity of the files in the archive without extracting them.
- update (-u) – Update existing files and create new ones if necessary.
- comment (-z) – Display only the archive comment.
- verbose (-v) – List files verbosely or show version information.
- exclude (-x) – Exclude specific files from extraction.
- extract to directory (-d) – Extract files into the specified directory.
- no overwrite (-n) – Never overwrite existing files.
- quiet mode (-q) – Operate quietly (use -qq for even quieter operation).
- overwrite (-o) – Overwrite existing files without prompting.
- case insensitive (-C) – Match filenames in a case-insensitive manner.
- lowercase (-L) – Convert filenames to lowercase when extracting.
Tips
As always, here are some bonus tips that can be useful when working with ZIP files on Linux.
Split your ZIP file into smaller parts
Before compressing an archive, you can use an option to split the compressed file into several smaller parts during the compression process that, when added together, will give you the total size of the compressed file. This is useful for moving or sharing a large compressed file to other people.
You can do this using the -s option followed by the value and size (b/k/m) of the parts you want the file to contain. All parts will have the indicated size except the last one, which contains the remainder of the total.zip -s <decimal-value>(b/k/m) <archive-file>.zip <files/directory>
For example:zip -s 900k rpi-test.zip sample-dir/
The process to decompress the archive is different when using unzip (in case of using another archive manager you can decompress the main zip part), you must concatenate the parts into a single part and then unzip in this way:zip -F <zip-splitted-file>.zip --output <merged-zip-file>.zip
unzip <merged-zip-file>.zip
This is an unorthodox method, but it is because the multi-part feature for decompressing it easily is not supported in unzip, so you have to resort to a manual method.
Choose between a faster compression or better compression
When performing the compression process you can choose the level at which the compression will be performed. Depending on the level, the compression will be faster or slower with a reduced size. By default, the compression will be performed at level 6.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
For fast compression, choose a low number (0 min). The lower the number, the faster the process will be, but the higher the final size of the compressed file will be since time will be prioritized over size reduction.zip -1 <zip-file>.zip <files>
In the opposite case, if you need a higher compression and a reduced compressed file, you can use a high number (9 max) for those cases. Note that this will increase the compression time and you may have to wait longer.zip -9 <zip-file>.zip <files>
Keep in mind that the compression level will be reflected more when you have large files, otherwise, it will not make much difference to the size of the compressed file.
Exclude/Include files according to your preferences
There are cases when you need to choose if you only want to include a specific set of files or if you want to exclude certain files from the ZIP archive. For those cases, you can use the EXCLUDE or INCLUDE options depending on your needs.
To exclude a file or directory, you can use the option -x, specify the name directory, or select a pattern. For example, you can use *.avi for specifying files of type avi. Let’s take a look at this example:zip <zip-file>.zip <files> -x <pattern>
You can apply the same for the process of decompressing a ZIP file, you can specify which type of file or directory to exclude:unzip <zip-file>.zip -x <exclude-pattern>
For the other case, if you need to include only a few files or directories out of many files, you can use the option -i to include them like this:zip <zip-file>.zip <files> -i <pattern-include>
These options are useful to avoid organizing files before compressing them and it helps to minimize the time spent on these tasks.
Alternatives
If you have the choice, ZIP is not the only way to deal with compressed archives, here are some other options to consider.
tar
Tar is a tool that is on many GNU/Linux distributions by default. It allows you to group different files and directories into a single file and keeps the data of the files intact without altering them.
The main difference with zip lies in its functionality, since it compresses the files, thus reducing their size, but the data of the files at the metadata level is compromised. You can see more about tar, in this article.
7z
7z is an archive manager focused on compressing files like zip, with the advantages of having a more refined compression, and more intuitive functions to use.
Both tools share most of the same functions, but zip falls behind in compression and support for certain functions, 7z can compress and decompress in the same tool and zip needs different tools (ZIP and UNZIP). You can see more about 7z in the following article.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Related Questions
How do I delete a file inside a Compressed ZIP?
If you mistakenly added a file in a ZIP archive or changed your mind about the content you want in the ZIP archive, you can delete any specific file or directory using -d option.
In this example, you can see the ZIP archive’s contents and the file that we will choose. First, let’s look at the files.
If you want to delete a specific file, you must use the -d option followed by the file you want to delete (you must know before which files are in the ZIP archive):zip -d <zip-archive>.zip <file-to-delete>
After doing all this, you must check the content of the compressed file with vi or less to make sure that the archive has been deleted before extracting or sharing it with someone else.
Is it possible to protect my Compressed ZIP?
Yes, it is possible to protect a compressed file using a password, which will be needed to decompress the file. There are two ways to protect the compressed ZIP: using an encryption prompt or providing the password directly in the line command.
For using encryption prompted in the command to protect the archive you will have to use the option -e and you will be prompted to enter the password securely:zip -e <zip-file>.zip <files>
On the other hand, you can protect the archive without using encryption and using the -P option instead. In this one, you can put the password in the same line of the command, like this:zip -P <password> <zip-file>.zip <files>
It is recommended that you always choose the first option and use the encryption prompt so that way your files will be protected, in case there is a problem with this option, you can choose the other method to provide the password in the command line, but it is not recommended.
What can I do if I need to update or add new files to an existing Compressed ZIP?
You can modify an existing file in the ZIP archive, using the -u option which will allow you to modify the file if there is a newer version on your computer. You can see an example like this:zip -u <zip-file>.zip <files-to-update>
In the same way, you can add new files to a ZIP archive, using the same -u option and choosing the new file you want to add, as you can see in the following example:zip -u <zip-file>.zip <file-to-add>
In both cases, you must verify the archive by checking its contents or extracting the archive and checking its contents are correct.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!
Additional Resources
Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.
VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.