List of content you will read in this article:
Zip is a widely used cross-platform command for data compression and archiving. Compression saves space by compressing data while archiving simplifies data transfer by merging several folders or folders into a single file. Consider the following scenario: if we need to send five files over the internet, each of which is 100 megabytes in size, sending each file one by one could take a long time. 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 in this guide, we will give you brief details about zipping and unzipping the files in Linux.
Installing Zip and Unzip
Ubuntu systems come with zip and unzip packages loaded by chance. You can conveniently install these if they are absent from your setup. Using the Ctrl+Alt+T keyboard shortcut, open the Terminal. Then, in Terminal, type the following to load zip:
$ sudo apt install zip
Run the following command in Terminal to install unzip:
$ sudo apt install unzip
You will be required to zip files 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
Unzipping a ZIP File in Linux
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:
The Linux-style ownership details are not supported by ZIP archives. The person who executes the command owns the collected data.
The folder where you're extracting the archive which is in zip format must have write permissions.
Suppress the unzip command's output
Unzip prints the names of all the files it's downloading by default, as well as a list when it's finished.
To prevent these messages from being printed, use the -q key.
unzip -q filename.zip
Unzip a ZIP Archive to a New Location
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 given command below:
sudo unzip installation.zip -d /var/xyz
We're using sudo in the 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 extracted files and folders are owned by root.
Unzip a ZIP file that has been password protected.
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:
[filename.zip] file.txt password:
As long as the password is valid, unzip can use it for all encrypted files.
When unzipping a ZIP file, exclude those files which 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/*"
Existing Files Would Be Overwritten
Let's pretend you've already unzipped a ZIP file and you're repeating the process:
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.
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.
Without Overwriting Existing Files, Unzip a ZIP File
Assume you've already unzipped a ZIP file and made modifications to certain files, but you've even erased a few files by mistake. You want to retain the updates and recover the files that were 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
Several ZIP Files Have To be Unzipped
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 command:
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.
List the Zip File's Contents all together:
Using the -l option to list the contents of a ZIP file:
unzip -l filename.zip
Unzip is a utility command that is used to list, test, and extract various compressed ZIP archives. The zip command is used to compress the files within the Linux operating system. Hence we have covered different operations related to unzip and understood how each varies depending on the usage of their command and wildcards.