List of content you will read in this article:
Compression saves space by compressing data while archiving simplifies data transfer by merging several files or folders into a single file. For example, if we need to send five files over the internet, each of which is 100 MB, sending each file one by one could take longer. It would be much faster to copy the files if they are compressed to a size of up to 50MB and then archive all of them in a single file than if they are not compressed. Unzip is used to retrieve and decompress zipped files. Zip is the most commonly used utility for archiving and compressing files. So this guide will give you brief details about Linux Unzip, how to extract files in Linux, and Unzip commands in Linux.
How to Install Zip and Unzip Utility
By default, Ubuntu systems come with zip and unzip packages. You can conveniently install these if not installed in your operating system. By using the Ctrl+Alt+T keyboard shortcut, open the Terminal. Then, type the following to load the zip:
$ sudo apt install zip
Run the following command in Terminal to install unzip:
$ sudo apt install unzip
You will be required to extract zip in linux in the following circumstances:
- Since zipping reduces the file size, it saves disc space.
- Using e-mail to forward huge files
- Increasing the speed of uploading or copying files
- In order to conserve bandwidth
How to Unzip in Linux? [Linux Unzip Command]
When used without any options, the unzip command removes all files from the designated ZIP folder to the current directory in its most basic form.
Let's presume you downloaded an installation ZIP file as an example. Simply execute the following command to unzip this file to the current directory:
unzip installation.zip
ZIP archives do not support Linux-style ownership details. The person who executes the command owns the collected data.
The folder where you're extracting the archive in zip format must have write permissions.
The output of the unzip linux command
Unzip prints the names of all the files downloaded by default, as well as a list when it's finished.
To prevent these messages from being printed, use the -q option.
unzip -q filename.zip
How to Unzip a ZIP file to a New Directory?
Using the -d option to unzip a ZIP file to a separate directory from the present one:
unzip filename.zip -d /path/to/directory
To unzip an installation folder file.zip to the /var/xyz/folder, for example, run the below given commands in linux:
sudo unzip installation.zip -d /var/xyz
We're using sudo command above because the user we're logging in as normally doesn't have write access to the /var/www directory. When you use sudo to decompress ZIP data, the root owns the extracted files and folders.
How to Unzip password-protected files?
If you want to unzip a protected file that is secured by a password, use the unzip command with the -P option and the required password for that file:
unzip -P ********(your password) filename.zip
Using the command line to type a password is vulnerable and should be avoided. Extracting the file normally without giving the password is a more safe choice. Unzip will ask you for the password if the ZIP file is encrypted:
unzip filename.zip
Output:
archive: filename.zip
[filename.zip] file.txt password:
Unzip can be used for all encrypted files as long as the password is valid.
Unzip a file by excluding those files that we don’t want to unzip:
Using the -x alternative accompanied by a space-separated list of archive files you wish to remove from being extracted to exclude individual files or folders from being extracted:
unzip filename.zip -x file1-to-exclude file2-to-exclude
Except for the.git directory, we remove all files and folders from the ZIP folder in the following example:
unzip filename.zip -x "*.git/*"
Unzip command will overwrite the Existing Files
Let's pretend you've already unzipped a ZIP file, and you're repeating the process:
unzip latest.zip
Unzip will prompt you to overwrite only the existing file, all records, skip the extraction of the existing file, skip the extraction of all files, or rename the current file by default.
Output:
Archive: latest.zip
replace wordpress/xmlrpc.php? [y]es, [n]o, [A]ll, [N]one, [r]ename:
Using the -o method if you want to erase current files without prompting:
unzip -o filename.zip
With this alternative, proceed with caution. Changes to the files will be missed if you make them.
Unzip command Without Overwriting the Existing Files
Assume you've already unzipped a ZIP file and modified certain files, but you've even erased a few files by mistake. You want to retain the updates and recover the files removed from the ZIP directory.
Using the command -n to force unzip to skip the extraction of a file that already exists:
unzip -n filename.zip
How to Unzip multiple files?
Regular expressions should be used to balance different files.
If you have several ZIP files in your current working directory, for example, you can unzip all of them with only one unzip command:
unzip '*.zip'
It's worth noting that the *.zip is enclosed in single quotes. If you don't quote the statement, the shell will enlarge the wildcard character, resulting in a mistake.
How to List the Zip File Contents together?
Using the -l option to list the contents of a ZIP file:
unzip -l filename.zip
Conclusion
It is a commonly used command to list and extract compressed ZIP files. The zip command in Linux is used to compress the files. When someone sends you a zip file, then you can unzip the Linux file to see the content. Hence we have covered different operations related to unzip and tried to help you to understand how each command varies depending on the usage.
People are also reading: