List of content you will read in this article:
There are many situations when we want to compress the files and store them on the system to save some space. The zip comes as a file compression utility that has been used on every operating system. It comes as a default utility for compressing files readily available on Windows operating system, Mac OS, and Linux OS. You can use all the compressed files as zip files. Also, if you create a zip file on one system, you can use these zipped files on another operating system without any compatibility issue due to the underlying operating system.
Here, we are using the Linux distribution, CentOS that uses the unzip command for opening the zipped files. In this article, we will learn how to play around with the unzip command on the CentOS 7 system.
How can you install the unzip utility on CentOS 7:
By default, there is no unzip package available on CentOS 7 system. But you can get it from the official package repository of the CenOS 7 operating system. So there is no problem with installing this package. To start the installation, first, you have to update the yum package repository cache by running the following command.
$ sudo yum make_cache
After this, you will get the updated YUM package repository cache as shown below.
After that, you can start installing the unzip package using the following command:
$ sudo yum install unzip
Then, you need to press the “y” and then hit <Enter> to continue.
This will definitely install the unzip package.
If you want to check if the unzip is working properly, then you can run the following command.
$ unzip -v
In the following image, you can see that the unzip utility is working properly.
Using Graphical User Interface to access zip files
If you are working with a graphical desktop environment like GNOME desktop, then you can easily use the zip files.
As you can see, below we have an app.zip file at the path ~/Downloads under the HOME directory.
When you right-click the zipped folder, then you will get various options mentioned below. Also, you will see the “open with archive manager” option. Once you double-click the zip file, by default it will get opened by the achieve manager.
Alternatively, you can also select the Extract Here to extract the zip file options as shown below.
Now, you can see that the zipped folder is being opened in the archive manager where you can see the list of contents of the zip file.
At the top of the archive manager, you will get the extract option. After clicking the extract option available at the top, you can see the content of the zip file has been extracted.
After that, you will be taken to the below window for extracting the zip file. From here, you can click the extract option on the top-right corner for extracting the zip file in the current directory. For extracting the zip file’s content to a new or another folder, you need to create a new folder accordingly.
Here, we have first created a new directory name as myapp/ as shown below and then we have started the extraction of the contents of the Zip file there.
Now, the contents of the Zip file are completely and successfully extracted into the myapp/ directory.
If you want, you can choose a specific file and folder from the Archive Manager and use its drag and drop functionality to place them in a directory. Only your required files and folders will get extracted.
Steps to list the Zip file’s contents
If you want to list all the contents of the files and directories present within the zip file, you can use the following command.
$ unzip -l app.zip
Now in the following image, you will get all the contents of the Zip archive app.zip.
Steps to extract a Zip File
For extracting the zip file, you can easily use the unzip command. But, in order to extract a zip file in the current directory containing the zip file, then you can use the following mentioned command
$ unzip app.zip
Once you run the above-mentioned command, you will get all the contents of the Zip file extracted to your current working directory.
According to the above image, all the files and directories will get extracted in the same path. Now we have seen the command format for extracting the zip file into the same directory, but for extracting the zip file’s content to another specified directory, then you can use the following command. But before that you have to make sure that you create the specified directory if it does not exist beforehand, running the command.
$ mkdir ~/Downloads/myapp
After you run the above command in order to create the directory, you can run the following command for extracting the Zip file app.zip into the ~/Downloads/myapp directory:
$ unzip app.zip -d ~/Downloads/myapp
The complete content present within the app.zip file will get extracted into the ~/Downloads/myapp directory as you can see in the screenshot below.
How to extract a Specific File from the Zip File using unzip Command:
If you want to extract the specific files and directories from the zip file, then you can do it using the unzip command. Suppose, you want to extract only the server,js file from the zip file, then you can use the following command format.
$ unzip app.zip server.js
This format will ensure that only the server.js will be extracted.
Also, you can even specify the specified directory where you want to extract the files and directories, using the following command.
$ unzip app.zip server.js -d ~/Downloads/myapp
If you want to extract a specific subdirectory from the Zip file, you can run the following command.
$ unzip app.zip 'models/*' -d ~/Downloads/mypp
Zip and unzip commands are very important when you are considering saving space even during the transmission of files from one system to another or via email. Linux distribution, CentOS 7 comes with the special command unzip for extracting the zipped file. You can use various command formats for specific purposes.
You can go through this guide to get along with the unzip command on CentOS 7.