ZIP files are a universal archive commonly used on Windows, macOS, and even Linux systems.
You can create a zip archive or unzip files from one with some common Linux terminal commands.
The .zip extension is used for the compressed file format.
This format allows you to compress all files of a directory without any data loss. Users can compress more than one directory or file at a time using Zip file format.
However, when you want to extract the zip files in Linux distribution, you need to use a command-line utility that is known as 'unzip command'.
You can easily extract all types of zip files just in a few minutes using this command-line tool.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux related queries.
In this context, we shall look into how you can unzip files through the terminal in a Linux environment.
We have implemented the unzip command on the CentOS 8 system.
These commands are the same for all Linux distributions, therefore, you can implement all these commands on Ubuntu, Debian, LinuxMint and fedora, and so on.
The unzip command is not installed on CentOS 8.
But, it is preinstalled on most of the Linux distributions.
Open the terminal window from the left sidebar of your desktop in CentOS 8 and install this utility by using the following command:
# yum install unzip
You can easily extract a zip file by using the unzip command.
Using the below-given command, you can unzip a file into a current folder or directory:
# unzip file-name
For example, if you want to extract a zip file 'text.zip' into the current directory 'Downloads' then, navigate into the 'Downloads' folder and list all files.
Now, use the following command to unzip a file:
# unzip text.zip
If you want to extract a zip file into another directory then, Use the -d option with the unzip command.
To extract a file into a different directory, type the below-mentioned command on the terminal:
$ unzip file-name.zip -d /directory-path
For example, you want to extract a zip file in the Documents instead of extracting it into the current Downloads.
So, in this case, the following command will be used:
# unzip test.zip -d /home/Documents/
During the extraction of a file, it first prints the file names and also shows a summary of task completion.
If you don't want to print all file names then, use the option '-q' with the unzip command as follows:
# unzip -q test.zip
Note: The 'text.zip' is the name of a zip file. You can replace your own file name.
Using the '-x' option with the unzip command, you can also exclude files and directories from extraction.
Use the following command to exclude a file while extracting a file:
# unzip file-name.zip -x exclude-name1 exclude-name 2
For example, we want to exclude the 'app' folder from the file extraction.
So, the above command will change into the following form:
# unzip test.zip -x "*app*"
If you have completed the extraction of a file and again type the unzip command then, it will ask you to overwrite or rename an existing file.
If you want to ignore this prompt then, type the following unzip command with option -o:
# unzip -o test.zip
Use the above-command carefully, due to any small mistake you can lose all your original data.
If you don't want to overwrite these existing files then using the option '-n', you can forcefully skip all these files which have been extracted or exist:
# unzip -n test.zip
You can also extract multiple files using the unzip command as follows:
# unzip '*.zip'
By using the following command, you can list the contents of a zip file:
# unzip -l myfile.zip
This article covers how to use the unzip command on the CentOS 8 Linux system.
Also, you will learn various uses of the unzip command through which you can list ZIP archive content and extract files. You can utilize the unzip command according to your needs.
How to Create a ZIP File with the zip Command ?
To create a ZIP file, you need to tell zip the name of the archive file and which files to include in it.
You don't need to add the ".zip" extension to the archive name, but it does no harm if you do.
To create a file called source_code.zip containing all the C source code files and header files in the current directory, you would use this command:
$ zip source_code *.c *.h
How to Unzip a ZIP File With the unzip Command ?
To extract the files from a ZIP file, use the unzip command, and provide the name of the ZIP file.
Note that you do need to provide the ".zip" extension.
$ unzip source_code.zip
To Unzip on the Linux command line:
The simplest option that will extract the contents to current directory:
$ unzip backup.zip
To change the target directory for extracted material, use -d option followed by the desired directory:
$ unzip backup.zip -d ./restore-directory
To preview contents of zip file:
$ unzip -l backup.zip
If you don't want to unzip the whole file, then add the specific files to extract at the end:
$ unzip backup.zip file1 subdirectory/file2
The inverse of the above command. Unzip every file EXCEPT the ones specified after the -x modifier:
$ unzip backup.zip -x file1 subdirectory/file2
Unzipping a password protected file:
$ unzip -p mypassword backup.zip