ZIP is the most popular and widely used to compress files and directories without losing the quality of the file. It supports lossless data compression. Archieved (zipped) files take less disk space on your system and can be transferred easily and faster from one system to another. ZIP files can be easily extracted in Windows, macOS, and Linux using the utilities available for all operating systems. In this tutorial, you will learn How to zip files and directories in Linux.
Install Zip Utility in Linux
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
Syntax of Zip Command
Following is the basic syntax for Zip command:
zip [options] [file_name] [files…]
- options : You can pass different zip options.
- file_name : Name of the zip file which you are creating.
- files : Files which need to be zipped
How to ZIP Files and Directories in Linux
You can create a simple zip file with zip command without passing any options. You just have to pass file names with space separated as given below:
zip bkpfile.zip file_name1 file_name2 file_name3
adding: file_name1 adding: file_name2 adding: file_name3
In above output you can see that it will print file names which are added to zip file.
Generally, we have need to compress a whole directory including content of subdirectories. The
-r option allows you to traverse the whole directory structure recursively. Use below command to use create a zip of a directory:
zip -r bkpfile.zip directory_name_or_path
You can also add multiple files and directories in the same archive:
zip -r bkpfile.zip directory_name1 directory_name2 file_name1 file_name2
Creating a Password Protected ZIP file
If you have private information that needs to be stored in the archive you can encrypt it using the
zip -e bkpfile.zip directory_name_or_path
It will prompt you to enter and verify the archive password:
Enter password: Verify password:
Creating Split Zip File
When you have large amount of files and directories and want to make zip of them then it’s difficult to manage a large single zip file. Instead of that you can split zip files using -s option followed by size.
For example you have data of 7 GB and want to split in 1gb of each then run the below command to split in multiples:
zip -s 1g -r bkpfile.zip directory_name_or_path
It should return the below output:
bkpfile.zip bkpfile.z01 bkpfile.z02 bkpfile.z03 bkpfile.z04 bkpfile.z05 bkpfile.z06 bkpfile.z07
This option is helpful when you want to upload file somewhere and there is file upload size limit.
You learned how to create ZIP archives using the zip command. To extract a ZIP archive on a Linux system, you can use the unzip command.
You can get more details about Zip at the Zip Man page. If you have any question or suggestion, please leave comment below.