The Zip command is one of the most used commands on Linux. The Zip command is used to compress one or more files and directories together into a single zip file. It reduces the size of the file and makes it easier to migrate. In other words, we can say a Zip file is a type of container which stores compressed files and directories. ZIP files use .zip or .ZIP extensions.
The Unzip command is also a mostly used command. The unzip command is used to extract the files and directories from a zip file.
In this tutorial, we will learn how to install the zip and unzip command, how we can create a zip of files and directories, and how we can extract the content of any zip file with the unzip command
Install Zip
For Ubuntu and Debian
sudo apt install zip -y
For CentOS and Fedora
sudo yum install zip -y
ZIP Files and Directories
To create a zip of files, specify the names of the files which you want to add to the archive. Use space to separate the names of the files.
zip name.zip filename1 filename2
After executing the above command, you will see an output like this:-

To hide the output of the zip command, simply use the -q attribute with the zip command.
zip -q name.zip filename1 filename2
To create a zip of the directory, including the content of subdirectories. Use the “-r” attribute to include the subdirectories. Run the following command.
zip -r name.zip Directory_name
To create a zip of multiple directories and files. Run the following command.
zip -r name.zip Directory_1 Directory_2 file_1 file_2
Compression Technique and Levels
Zip command by default uses deflates compression technique to compress the files. If the zip is unable to compress the files, then it simply stores all the files without compression. The Zip command also supports the bzip2 compression technique.
To use the compression technique, then simply add the -Z attribute with the Zip command.
zip -r -Z bzip2 name.zip Directory_name
Create a Password-Protected ZIP file
If you want to use password and encryption on the zip to protect the content of the zip, then simply use the -e attribute with the Zip command.
zip -e name.zip Directory_name

Create a Split Zip File
A split is a useful option in the Zip command. It is used to create a Zip of a big file into multiple small Zip. We can use the split option with the -s attribute followed by the size. We can define the size in KB (kilobytes), MB (megabytes), GB (gigabytes), or TB (terabytes).
zip -s 1g -r name.zip Directory_name
After executing the above command, you will see an output like this:-

Install Unzip
For Ubuntu and Debian
sudo apt install unzip -y
For CentOS and Fedora
sudo yum install unzip -y
Unzip a ZIP file
To extract a Zip file.
unzip new.zip

To hide the output of the unzip command, simply use the -q attribute with the zip command.
unzip -q name.zip
List the contents of a Zip File
To list all the contents of the zip, run the following command.
unzip -l name.zip
For example, We want to list all the contents of wordpress.zip
unzip -l wordpress.zip
Unzip a ZIP File to a Different Directory
If you want to extract the zip to a different directory than the current one, then use the -d attribute with the unzip command.
unzip name.zip -d /mention/the/path
For example, to unzip the wordpress.zip to /home/centos/ directory.
unzip wordpress.zip -d /home/centos

Exclude Files when Unzipping a ZIP File
If you want to exclude specific files or directories from being extracted, use the -x attribute followed by the list of files you want to exclude from extracting.
unzip name.zip -x exclude_file1 exclude_file2
For example, we have to exclude the index.php file and wp-content directory from the WordPress zip.
unzip wordpress.zip -x "*index.php*" "*wp-content*"
Conclusion
In this tutorial, we have learned about the Zip and Unzip commands with their useful attributes. Zip and Unzip are the most useful commands on Linux.
If you guys have any queries, then let me know in the comments sections.
Leave a Reply
View Comments