The zip archive format is a compressed file format which supports loseless compression. You can compress a file or a complete directory into a zip archive. The zip archive format is supported by almost every operating system inlcuding the most popular ones like Windows OS, Linux OS, macOS etc. In this tutorial we will learn how to install the the zip and unzip utilities in our Linux machine and then we will learn the various commands that we can use to zip and unzip files and directories with examples.
You need the sudo or root privilige for installing zip and unzip utilities on your Linux machine.
Install Zip and Unzip on Linux
Linux operating system has many flavours like Debian, CentOS, Fedora, Ubuntu, etc. Different operating systems have different way of installing utilities. Ubuntu and Debian use apt to manage software installation and update. The below specified command will install zip and unzip on your Ubuntu or Debian machine:
sudo apt-get update
sudo apt-get install zip unzip -y
To install zip and unzip in CentOS or Fedora machine, we will use the yum utility. Below is the command which can be used for the installation:
sudo yum update
sudo yum install zip unzip
After running the above commands it should take 10-20 seconds for successful installation. Once the installation completes, you can run
unzip command to see if they are installed or not.
How to Zip file/directory in Linux
Following is the syntax for using the
zip command once its properly installed.
zip [options...] name-of-file.extension [FILES...]
This is the basic syntax of using the zip command to create a zip archive of a file. We can provide different options for the zip command along with a name for the zip archive that will be created followed by the files that you want to archive. You can archive a single file, multiple files or a complete directory using the zip command.
1. Compress Individual Files
If we have multiple individual files like file1.txt, file2.csv, etc, that we want to compress inside a single zip archive file then we can use the following command:
zip myarchive.zip file1.txt file2.csv
This command will create a zip archive with name myarchive.zip with the two files specified in the current directory.
2. Compress a Directory
To compress a directory along with its contents(files, sub-directories, etc), we use the
zip command along with the
-r option which means that all the files inside the directory, along with the files present in its sub directories recursively will be included in the zip archive file.
For example, if we have a directory /home/studytonight which has some files, and some directories with further files and more sub direstories, following command can be executed to zip the entire content into a single compressed zip archive file with name studytonight.zip:
zip -r studytonight.zip /home/studytonight
In the above command we have specified the complete path to the directory which I want to zip, in case you are present in that directory, which means you want to zip all the files present in the current directory, then you can execute the following command:
zip -r somearchive.zip *
The above command will create a zip file compressing all the files present inside the current directory. Here,
*(asterisk) refers to all the files present in the current directory.
How to Unzip in Linux
To unzip files from a zip archive we will use the unzip utility. Following is the basic syntax of the unzip command:
$ unzip [OPTIONS] [ZIP_ARCHIVE] [FILES_TO_PROCESS] [-x FILES_TO_EXCLUDE]
Just like the zip command, we have many different options to use along with the basic unzip command. Also, along with providing the name of the zip archive that we wish to unzip, we can provide names of files to unzip or to exclude from the unzipping process. Let's learn how to use the unzip command with a few examples.
1. Verify Validity of a Zip archive
Before unzipping any zip archive, its good practice to first validate the zip archive. Let's say you have received or you have downloaded a zip file somearchive.zip from the internet and you want to validate it before unzipping its content, you can do so using the unzip utility itself, by executing the following command:
$ unzip -tq somearchive.zip
For a valid zip archive file, the output of the above command should be,
No errors detected in compressed data of somearchive.zip
2. List all the files in Zip arhive
Once you have validated a zip archive file, you can even list all the files and directories present inside the zip archive without extracting them. If we use the
-l option along with the unzip command, we will get the complete list of all the files present inside the zip archive file, along with directories, sub-directories and the files inside these directories, recursively.
For an archive file with name somearchive.zip, you can execute the following command to list down the name of the files and directory inside this zip,
unzip -l somearchive.zip
3. Unzip a Zip archive
To unzip a zip archive file with name somearchive.zip, we can run the following command:
If you want to unzip the content of the zip archive file into a specific directory, you can do so by using the
-d option alonwith specifying the name of the directory. In the command below, we have speocifed that the content of the zip archive studytonight.zip should be unzipped in the directory /home/studytonight.
unzip -d /home/studytonight studytonight.zip
4. Unzip an archive except a few files
If you want to exclude some files present in the zip archive during unzipping you can specify the file names to exclude from unzipping using the -x option while executing the unzip command,
unzip smearchive.zip -x notrequired.txt
In this tutorial you learned about the zip and unzip utilities present in Linux operating system which you can use for creating zip archive files or for unzipping zip archive files. Creating zip archive files for packaging is a good way to compress files and directories into a single file, the process of zipping is loseless and makes it easier to share as there is a single compressed zip archive file.
You may also like: