Read in 4 minutes
How to Zip Files and Directories in Linux
ZIP is the most widely used archive file format that supports lossless data compression.
A ZIP file is a data container containing one or more compressed files or directories. Compressed (zipped) files take up less disk space can be transferred from one to another machine more quickly than uncompressed files. ZIP files can be easily extracted in Windows, macOS, and Linux using the utilities available for all operating systems.
In this tutorial, we will show you how to zip (compress) files and directories in Linux systems through the command line using the zip utility.
Zip is a utility that helps you create ZIP archives.
The zip command takes the following syntax form:
zip OPTIONS ARCHIVE_NAME FILES
To create a ZIP archive in a specific directory the user needs to have write permissions on that directory. ZIP files do not support Linux-style ownership information, and all extracted files will be owned by the user that runs the command. If you want to preserve the file ownership and permissions use the tar command.
The zip utility is not installed by default in most Linux distributions, but you can easily install it using the package manager of your distribution.
Install Zip on Ubuntu and Debian
sudo apt install zip
Install Zip on CentOS and Fedora
sudo yum install zip
How to ZIP Files and Directories
To zip one or more files, specify the files you want to add to the archive separated by space as shown below:
zip archivename.zip filename1 filename2 filename3
adding: filename1 (deflated 63%) adding: filename2 (stored 0%) adding: filename3 (deflated 38%)
By default the zip command prints the names of all the files that are added to the archive and the compression method. We’ll explain the compression methods and level later in this guide.
When specifying the zip archive name if you ommt the
.zip extension it will be added automatically unless the archive name contains a dot.
zip archivename.zip filename will create an archive with the same name as would
zip archivename filename.
If you want to suppress the output of the zip command use the
zip -q archivename.zip filename1 filename2 filename3
Most often, you’ll create ZIP archive of a directory including the content of subdirectories. The
-r options allow you to traverse the whole directory structure recursively. To create a zip archive of a directory you would use:
zip -r archivename.zip directory_name
You can also add multiple files and directories in the same archive:
zip -r archivename.zip directory_name1 directory_name2 file1 file1
Compression Methods and Levels
The zip default compression method is deflate. If the zip utility determines that a file cannot be compressed it simply stores the file in the archive without compressing it using the store method. In most Linux distributions the zip utility also supports the bzip2 compression method.
To specify a compression method use the
zip -r -Z bzip2 archivename.zip directory_name
... adding: sub_dir/ (stored 0%) adding: sub_dir/file1 (bzipped 52%) adding: sub_dir/file2 (bzipped 79%)
The zip command allows you to specify a compression level using number prefixed with a dash from 0 to 9. The default compression level is
-6. When using
-0 all files will be stored without compression.
-9 will force the zip command to use an optimal compression for all files.
For example, to use the compression level
-9 you would type something like this:
zip -9 -r archivename.zip directory_name
The higher the compression level, the more CPU-intensive the zip process is and it will take more time to complete.
Creating a Password Protected ZIP file
If you have sensitive information that needs to be stored in the archive you can encrypt it using the
zip -e archivename.zip directory_name
You will be prompted to enter and verify the archive password:
Enter password: Verify password:
Creating Split Zip File
Imagine you want to store the zip archive on a file hosting service that has a file size upload limit of 1GB and your ZIP archive is 5GB.
You can create a new split Zip file using the
-s option followed by specified size. The multiplier can be k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes).
zip -s 1g -r archivename.zip directory_name
The command above will keep creating new archives in a set after it reaches the specified size limit.
archivename.zip archivename.z01 archivename.z02 archivename.z03 archivename.z04
Create a ZIP archive named archivename.zip containing all the files in the current directory.
zip archivename *
Same as above including the hidden files (files starting with a dot):
zip archivename .* *
Create a ZIP archive named archivename.zip containing all MP3 files in the current directory without trying to compress the files.
zip -0 archivename *.mp3
In this tutorial you learned how to create ZIP archives using the zip command. We have also shown you the most common options of the zip command. To extract a ZIP archive on a Linux system, you can use the unzip command.
If you want to learn more about Zip visit the Zip Man page.