Zipping a file or directory is a popular practice when you want to send a huge file that seemingly takes up a lot of space. Zipping a file comes recommended when uploading the file/directory or sending it via email. Unzipping is the opposite of zipping. It decompresses the file allowing you to access its contents. In this guide, we will see how you can unzip a file in Linux.
Unzip a file in Linux
Having discussed a little bit about zipping and unzipping files, let’s now roll our sleeves and see how you can unzip files. On the terminal, the command-line utility used for unzipping files from an archive is the unzip command. In its most basic form, the syntax for unzipping an archive file is as shown:
$ unzip archive.zip
Installing unzip in Linux
Before we proceed to show you how to unzip files or directories, we need to point out that while the unzip command-line tool comes preinstalled in most modern systems, it is not native to some distributions. Let’s see how to install unzip command.
CentOS / RHEL
For RHEL & CentOS flavors and their derivatives, run the following command:
$ sudo yum install unzip
Ubuntu / Debian
For Debian / Ubuntu variants, invoke the command:
$ sudo apt-get install unzip
Arch/ Manjaro Linux
$ sudo pacman -S unzip
For Fedora systems run:
$ sudo dnf install unzip
By now, we have covered how to install unzip in most of the Linux distributions. Let’s now walk you through the process of unzipping files and directories using the unzip utility.
How to unzip an archive file
We have earlier seen the basic syntax of unzipping an archive.
$ unzip archive.zip
For example, to unzip Nextcloud’s installation ZIP file, run:
$ unzip nextcloud-19.0.3.zip
Unzipping is pretty fast and you will see similar output as shown.
Extracted files are owned by the Linx user that extracted the files. For example, if the zipped file was downloaded and extracted by the logged-in user ‘Winnie‘, then the uncompressed file or directory is owned by the same user.
How to unzip a file to a different directory
As seen in the previous example, the unzip command retains both the archive and the decompressed file will be in the same directory.
If you desire to extract or unzip your archive file to a different destination, use the -d flag to specify the destination path as shown.
$ unzip archive.zip -d /path/to/destination
For example, to unzip the Nextcloud archive file to the
directory, run the following command:
$ sudo unzip nextcloud-19.0.3.zip -d /var/www/html
Also note that when you are unzipping to a directory that you don’t have permissions over, use the sudo command to obtain elevated privileges as shown above.
To confirm that the operation was successful, use the
command to verify the presence of the unzipped file.
$ ls -l /var/www/html/
From the output, we have verified that the Nexcloud archive was successfully extracted to the specified destination path.
Suppress output during the unzip operation
So far, we’ve seen a flurry of output during the unzipping operation. If you wish to have a ‘silent’ terminal and simply unzip without being bombarded by the output on the terminal, use the -q option to suppress printing out these tons of messages.
$ unzip -q archive.zip
For example, using our Nextcloud archive, run the command:
$ unzip -q nextcloud-19.0.3.zip
List contents of a zipped file
If you have no clue or are unsure about the contents of an archive but wish to list what’s contained therein, use the -l option (For list) to give you a summary of the contents of the zipped file.
$ unzip -l archive.zip
Exclude a file when unzipping
Once you have an overview of the contents of the archive file, you may wish to exclude some files from being unzipped. To accomplish this fete, use the -x switch followed by the list of files to be excluded separated by a space.
$ unzip archive.zip -x file1 file2 file3
Overwrite a file while unzipping
Suppose you have already unzipped your files and you are once again, running the command again. When this happens, you will be prompted whether you want to overwrite the file, skip the extraction, rename of skip the extraction of all files as indicated.
Naturally, you would press ‘y’ for yes to overwrite the current file with the new one. However, to simplify this, simply pass the -o flag to overwrite the current file without being prompted on the terminal.
$ unzip -o filename
This way, you will overwrite the file without any prompts.
Unzip multiple archive files simultaneously
If you have several zipped files and you want to unzip them all at the same time, use the wildcard symbol as shown in the syntax below.
$ unzip '*.zip'
Take extra note of the single quotes ” around the wildcard. If you leave them out, you will certainly get an error.
In this tutorial, we walked you through the process of unzipping archive files using the unzip command. As you have seen unzipping files is quite a breeze and not complex. And this draws the curtain to our tutorial. We hope you found this guide on how to unzip a file in Linux valuable and that going forward, you will be able to unzip files without a hitch.