Here’s a beginner’s guide on understanding the zip command in Linux with a few examples.
A zip file is a compressed archive containing one or more files. It is widely used as a lossless data compression technique. Thanks to compression, it takes less disk space and requires fewer data to transfer over computer networks.
These compressed files can be extracted easily in Linux, Windows and macOS. Various software is available that supports zip file compression and also offers to extract them.
Since it is popular, almost all operating systems have this built-in.
In this tutorial, we will talk about several terminal-based methods to zip a file in Linux.
Table of Contents
Zip Command in Linux: Examples
The program name you need to use in Linux to zip a file is
zip. Here’s the basic syntax.
zip [compress file name] file1 file2 file3
And here’s the official syntax.
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
Ideally, it should be installed in all the major Linux distributions. If not, use the following commands to install it.
Install zip on Debian, Ubuntu and related distributions
sudo apt install zip
Install in Fedora, RHEL-based systems
sudo dnf install zip
pacman -S zip
Let’s take a look at some examples.
How to zip files and folders
I have the following three files in my test directory. They are file1.txt, file2.txt and file3.txt. If I want to compress three files using zip and create a zip myfiles.zip, the following command is sufficient.
zip myfiles.zip file1.txt file2.txt file3.mp3
adding: file1.txt (stored 0%)
adding: file2.txt (stored 0%)
adding: file3.mp3 (deflated 13%)
A few points you should remember here.
- When creating a zip file, you should have the modify access to the current directory.
- The zip file format doesn’t contain the permissions, i.e. read(4), write(2), and execute(1). So, the user who creates it becomes the owner of the file.
- If you want to use zip with permission, try the tar command (to be explained in a later tutorial).
- In the above output, the zip command shows the file names being added to the archive and the compression method.
- Specifying the .zip file name extension in the target file name is not mandatory. If you omit .zip, zip will add the .zip at the end.
When you have hundreds or thousands of files in operation, it’s a good idea to suppress the output in the terminal. You can use -q command to suppress the output in the zip command.
zip -q myfiles.zip file1.txt file2.txt file3.txt
Recursive compression of subfolders
-r option of zip command enables you to include subdirectories and their contents. This option recursively traverses until the last child of a directory structure and adds all of them to the zip file.
The following command creates a compressed file with all the contents and subdirectories inside my_folder.
zip -r myfolder.zip my_folder
You can also use the wild card characters (*) to include specific types of files in your zip file.
zip -0 my_movies.zip *.mp4
Adding a mix of files and directories
With all the above options, the zip command allows you to specify files and directories together as arguments.
zip -r myfiles.zip file1.txt file2.txt file3.txt my_folder1 my_folder2
The default output of the zip file contains two distinct words, i.e. deflate and store. The default compression method used by zip is deflate. If it successfully compresses the file, then the output shows deflate. And when it can’t compress a file, it simply stores them inside the .zip file as is. Those file outputs show stored.
There any many compression algorithm present. One of them is the bzip2 compression method which is supported by zip command in Linux. You can specify the compression algo as a command option to use. Use the option -Z followed by the algorithm name, as shown below.
zip -r -Z bzip2 myfolder.zip my_folder
The zip command also allows you to specify the compression level. The compression level is how much you want the zip optimized to reduce the package size. It’s a numeric value range from 0 to 9. A value of 9 compression level is the highest compression. The default value is 6.
Remember, if you are using zip to compress thousands of files of varying sizes, it might use higher system resources and take time. So, if you use it in programs, or shell scripts for many files, follow proper programming standards.
zip -9 -r myfolder.zip my_folder
Password protect a zip file
You can also password protect a zip file using
-e option like the one below.
zip -e -r myfolder.zip my_folder
It will ask for the password after you run the command.
Caution: Don’t use the zip command to password-protect a zip file. The encryption algorithm of the zip is PKZIP using a stream cipher. And it can be cracked easily. If you want to protect your file, use 7-Zip or other advanced tools.
Split size zip files
Many applications, servers, and file shares may contain restrictions of a fixed-size file upload. For example, you have a 10GB file, but the service allows only 1GB per file. Using the -s option of the zip, you can compress and split them into chunks for upload.
zip -s 1g -r myfolder.zip my_folder
You learned some basics of the zip command. It is useful for most local cases where you need to take quick backups by compressing them on the fly. However, for more advanced options, you should use 7-Zip or other commands, which I will share in the next few articles.
In the meantime, you can learn more in the zip manual.
This article is part of the Linux command learning series.