Introduction
Before we begin talking about how to zip files and directories on Linux, let's briefly understand – What is Zip File?
A zip file is a compressed folder containing one or more files or directories. It reduces the overall file size to save storage space and enable faster file transfer. Zip files are commonly used for packaging and distributing multiple files or for compressing large files into a more manageable size.
This file format is compatible with various operating systems and can be easily opened with popular archive software. With its ability to efficiently compress files, zip files have become an essential tool for organizing and sharing large amounts of data.
In this tutorial, you will zip files and directories on Linux. We will also address a few FAQs on how to zip files and directories on Linux.
Advantages of Zip File
- Efficient Compression: Zip files significantly reduce file sizes, saving storage space and enabling faster transmission.
- File Bundling: Multiple files or directories can be easily bundled together into a single zip file, making it convenient for organization and sharing.
- Simplified File Transfer: With reduced file sizes, zip files can be quickly transferred over the internet or via email.
- Data Integrity: Zip files support error-checking mechanisms, ensuring that files remain intact during compression, decompression, and transmission.
- Cross-Platform Compatibility: Zip files are compatible with various operating systems and can be opened using popular archive software.
zip
Command
zip is a command-line tool that aids in the creation of Zip archives.
The syntax for the zip
command is as follows:
zip OPTIONS ARCHIVE_NAME FILES
A user must have write permissions on a directory in order to build a Zip archive in that directory.
In the Linux approach, zip files do not enable ownership information. The user who runs the command owns the extracted files. To maintain file ownership and permissions intact, use the tar command.
Most Linux distributions do not include the zip
program by default, but you may easily install it using your distribution's package management.
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, separate the files to be included in the archive by a space, as illustrated below:
zip archivename.zip filename1 filename2 filename3
Output
adding: filename1 (deflated 63%)
adding: filename2 (stored 0%)
adding: filename3 (deflated 38%)
The zip
command publishes the names of the files that have been added to the archive as well as the compression method by default.
Unless the archive name contains a dot, the extension is automatically appended if the archive name does not conclude with .zip
. zip archivename .zip filename
creates the same archive as zip archivename filename
.
To suppress the output of the zip command, use the -q
option:
zip -q archivename.zip filename1 filename2 filename3
Creating a zip archive of a directory that includes the content of subdirectories is common. The -r
option allows you to recursively traverse the entire directory structure:
zip -r archivename.zip directory_name
In the same archive, you can also add numerous files and directories:
zip -r archivename.zip directory_name1 directory_name2 file1 file1
Compression Methods and Levels
Deflate is Zip's default compression algorithm. If a file cannot be compressed, the zip
utility just stores it in the archive without compressing it using the store method. In most Linux distributions, the zip
software supports the bzip2 compression method.
Use the -Z
option to select a compression method.
zip -r -Z bzip2 archivename.zip directory_name
Output
...
adding: sub_dir/ (stored 0%)
adding: sub_dir/file1 (bzipped 52%)
adding: sub_dir/file2 (bzipped 79%)
The zip
command lets you define a compression level from 0 to 9 by providing a number prefixed with a dash. The compression level is set to -6
by default. When -0
is specified, all files are saved without compression. -9
forces the zip command to perform the best compression possible for all files.
If you wanted to use compression level -9
, for example, you would type:
zip -9 -r archivename.zip directory_name
The zip
operation becomes more CPU-intensive when the compression level is increased, and it takes longer to complete.
Creating a Password Protected ZIP file
If you have sensitive data that has to be stored in the archive, use the -e
option to encrypt it:
zip -e archivename.zip directory_name
Using the following command, the archive password must be entered and verified:
Output
Enter password:
Verify password:
Creating Split Zip File
Assume you want to upload the Zip archive to a file hosting provider with a 1GB upload limit, and your Zip archive is 5GB in size.
The -s
option, followed by a size, can be used to create a new split Zip file. k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes) can be used as the multiplier (terabytes).
zip -s 1g -r archivename.zip directory_name
After a set hits the stated size limit, the operation above will keep producing new archives in it.
Output
archivename.zip
archivename.z01
archivename.z02
archivename.z03
archivename.z04
zip
command Examples
Create an archivename.zip
file that contains all of the files in the current directory.
zip archivename *
As before, including secret files (those beginning with a dot):
zip archivename .* *
Create an archivename.zip
file that contains all MP3 files in the current directory without compressing them.
zip -0 archivename *.mp3
FAQs to Zip Files and Directories on Linux
How do I extract a zip file on Linux?
To extract a zip file, use the command "unzip" followed by the name of the zip file you want to extract. Example: unzip archive.zip
Can I password protect a zip file?
Yes, you can add a password to a zip file for added security using the "-P" option when creating the zip file. Example: zip -P password archive.zip file1.txt
How can I list the contents of a zip file without extracting it?
Use the command "unzip -l" followed by the zip file name to list the contents of a zip file without extracting it. Example: unzip -l archive.zip
How do I update an existing zip file with new files?
To update an existing zip file, use the command "zip" with the "-u" option followed by the zip file and the files or directories you want to add. Example: zip -u archive.zip newfile.txt
How can I split a large zip file into smaller parts?
To split a large zip file, use the command "split" followed by the size and name of the output parts. Example: split -b 100M archive.zip archive.zip.part
How do I merge split zip files back into a single file?
To merge split zip files, use the command "cat" followed by the names of the parts and redirect the output to a new zip file. Example: cat archive.zip.part* > merged.zip
Are zip files compatible with other operating systems?
Yes, zip files are widely compatible across different operating systems, including Linux, Windows, and macOS. They can be opened and extracted using various archive software available on each platform.
Conclusion
The zip
command in Linux can be used to build Zip archives.
The unzip command can be used to extract a ZIP archive on a Linux system.
Visit the Zip Man page for more information on the zip command.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.