How to zip and unzip in Linux with Examples
A Zip archive is a compressed file format that supports lossless data compression. A zip archive might contain multiple files and directories inside it. Zip is a very popular archive file format. Almost every operating system supports zip including all the operating systems by Microsoft as well as all the Linux operating systems. In this in-depth guide, I will show you how to install zip and unzip on Linux operating systems. We will also learn how to zip and unzip files in Linux.
In order to use Zip and Unzip in Linux, we have to first install Zip and Unzip utilities on our Linux operating system. Before we move on to the installation part, make sure you have sudo privilege on your Linux server. We need sudo privilege to install new software packages on our server.
So, If you have sudo privilege, you can continue with the tutorial.
Install Zip and Unzip on Ubuntu/Debian
It is very easy to install these utilities on Ubuntu and Debian. Both these operating systems use
apt to manage software on the machine. Execute the following commands to install zip and unzip utilities on Ubuntu/Debian operating systems.
$ sudo apt-get update $ sudo apt-get install zip unzip -y
It will just take 10-20 seconds to install these utilities. Once the process is complete, execute the
zip command to verify the installation. You will get list of options and the version of zip as an output.
Install Zip and Unzip on CentOS
This method will also work for Fedora operating system as both of them use
yum to manage software on the machine. Execute the following commands on your server to install Zip and Unzip on CentOS server.
$ sudo yum update $ sudo yum install zip unzip
Once the process is complete, execute the
zip command to verify the installation. Once the installation is done, we can move on to learn how to zip and unzip in Linux operating systems.
How to Zip in Linux
Zip is a very simple and straightforward utility. Let us first see the syntax of zip command.
$ zip [OPTIONS...] ZIP_FILE_NAME [FILES ...]
Options in zip command allows us to control the execution of the command. Then comes the ZIP_FILE_NAME, we have to replace this with the name of the zip file we want to create, for example,
example.zip. Then, you can list all the files that you want to include in this zip archive.
COMPRESS INDIVIDUAL FILES
For example, Let’s say we have file1.txt and file2.csv that we want to zip. To compress these files inside a zip archive, we have to run the following command.
$ zip files.zip file1.txt file2.csv
This command will create a zip archive called
files.zip in the current directory.
COMPRESS A DIRECTORY
Now, Let’s say we have a directory called
wordpress that we want to compress in a zip archive. To create a zip archive of a directory, we will use the
-r option in zip command. The
-r option will make the process recursive, it means that the zip archive will contain every file and subdirectories contained inside the directory we want to compress.
Here is how we can compress our
wordpress directory including all the files and directories inside it.
$ zip -r wordpress_backup.zip wordpress
This command will create a
wordpress_backup.zip archive in current directory containing all the files and directories and sub-directories of
COMPRESS EVERYTHING IN CURRENT DIRECTORY
If you want to compress everything in the current directory, navigate to the directory and execute the following command.
$ zip -r backup.zip *
* stands for wildcard character. It means that it will match all the directories and files inside the current directory. Now, Let us see how to password protect our zip archive.
PASSWORD PROTECT A ZIP ARCHIVE
Zip also allows you to password protect a zip archive. However, this feature is not recommended because the password we will pass while executing the command will be in a plain text format. It is good only if no one is looking over your shoulder.
To create a zip archive of our example
wordpress directory with password protection, execute the following command.
$ zip -r --password=PASSWORD wordpress
This command will create a password protected zip archive containing our example wordpress directory with all the files inside it. It will ask for a password while using the
How to unzip in Linux
Unzip is an utility in linux like zip. Unzip utility allows us to extract files from a zip archive. The simple syntax of unzip command is as follows.
$ unzip [OPTIONS] [ZIP_ARCHIVE] [FILES_TO_PROCESS] [-x FILES_TO_EXCLUDE]
The options allow us to control the execution of the command. After options, we have to specify the name of the zip archive that we want to extract. Then comes the two optional sections in which you can specify the files that you want to process and the files that you want to exclude.
TEST VALIDITY OF ZIP ARCHIVE
For example, Let’s say we have a zip archive called
wordpress.zip that we want to extract. But first, we want to test the zip archive validity. To test the validity of our
wordpress.zip archive, execute the following command.
$ unzip -tq wordpress.zip
The output of the command should look like the following if the zip archive is valid.
Output: No errors detected in compressed data of wordpress.zip.
LIST ALL THE FILES INSIDE ZIP ARCHIVE
Now, to take a look at all the files inside the zip archive, we can use the
-l option. So, to get list of all the files and directories inside our zip archive without extracting it, we have to execute the following command.
$ unzip -l wordpress.zip
This command will give you the list of every file and directory compressed in the archive. It will also list files inside directories and sub-directories as the function is recursive.
UNZIP A ZIP ARCHIVE
Now, Let's extract our example
wordpress.zip archive. To extract the full archive, execute the following command.
$ unzip wordpress.zip
UNZIP A ZIP ARCHIVE EXCEPT FEW FILES
Now, Let's say we want to extract all the files, but we do not want our
wp-config.php file from our WordPress backup because we want to create a new file. In that case, we can use
-x option to specify the list of files we want to ignore. To do this, we have to execute the following command.
$ unzip wordpress.zip -x wp-config.php
This command will extract all the files from our example wordpress zip archive, except
wp-config.php file. So, this is how we can use the unzip utility to extract zip archives in linux based operating systems. Just like zip and unzip, you can use tar command in linux to extract and compress files in linux.
Conclusion: Zip and Unzip has many more functions that you can use to manage your zip archives. For example, you can use the
-d option in
unzip command to specify the directory you want to extract from a zip archive. It will only extract the specific directory. Execute
man zip or
man unzip to learn more options provided by zip and unzip respectively.
If you are stuck somewhere in the process, please let us know in the comment section given below. We will respond as soon as possible with the solution. If you are an existing InterServer customer, please contact our support staff to resolve your queries instantly!