Linux
How to unzip a zip file on Linux [2023]: The Ultimate Guide
Let’s Learn How to unzip a zip file on Linux with Practical Examples :
In this guide, you will learn how to unzip a zip file on Linux. So what’s a zipped file? This is a file that is compressed to save on the space it occupies. In addition, zipped files are easier to send as email attachments and also to download from websites.
You can identify a zipped file using the .zip file extension. Unzip is a command-line utility used to unzip or decompress files with a .zip file extension. Let’s have a look at various ways you can use and how to unzip a zip file on Linux using those methods.
In summary, we will cover the following:
- Install unzip command in Linux
- Unzip a zip file on Linux
- How to zip files/directories in Linux
- Install zip command in Linux
Install unzip command in Linux
While the unzip package may come installed in most modern Linux distributions, it may not ship with some Linux flavors by default. But that’s not a big issue. You can easily install the unzip package as shown:
Install unzip in Ubuntu/Debian distributions
If you are working on a Debian-based system, you can install zip by running the command:
1 |
$ sudo apt install unzip |
Install unzip in RHEL / CentOS
If you are running a RedHat-based distribution such as CentOS and RedHat, execute the commands below to install unzip:
1 |
$ sudo yum install unzip |
Install unzip in Fedora
For fedora systems, execute the command below:
1 |
$ sudo dnf install unzip |
Install unzip in ArchLinux / Manjaro
For Arch-based systems such as Manjaro, install unzip using the Pacman package manager as shown:
1 |
$ sudo pacman -S unzip |
Install unzip in OpenSUSE
To install unzip in OpenSUSE systems, run the command:
1 |
$ sudo zypper install unzip |
How to unzip a zip file on Linux
In its most basic form, without any arguments, the unzip tool is used for unzipping files in Linux. The basic syntax is shown below:
1 |
$ unzip file.zip |
For example, to unzip a file called nextcloud-18.0.3.zip
downloaded from Nextcloud, execute the command:
1 |
$ unzip nextcloud-18.0.3.zip |
Note that the user who has unzipped the file owns the extracted directory and files therein. For example, if a user ‘Winnie’ has unzipped a file, then the directory and all the extracted contents will bear Winnie’s ownership rights as shown in the images below.
In addition, you must have the requisite permissions needed to unzip a file that is not owned by the user. Otherwise, if you are a regular user, make use of the Sudo command to get administrative privileges to unzip the file.
Unzip multiple files in one command
If you have several zipped files in a directory, you can unzip them simultaneously using one command. This is done using a wildcard symbol (*). The syntax is shown below:
1 |
$ unzip '*.zip' |
Unzip files to a different directory
You can unzip a file to a different directory using the -d option followed by the destination directory as shown:
1 |
$ unzip file1.zip -d /path/to/destination/directory |
Using our Nextcloud file, we are going to unzip it to the /var/www/html
directory which is the webroot directory. To achieve this, run:
1 |
$ sudo unzip nextcloud-18.0.3.zip -d /var/www/html |
Note that this time we have used sudo because the regular user ‘Winnie’ does not have write permissions to the destination. When unzipped, the uncompressed files are in effect owned by the root user.
Suppress verbose output of unzip
Usually, when unzipping a file, the extracted files are displayed on the terminal during the unzipping process. You can choose to omit the display of these files using the -q option as shown:
1 |
$ unzip -q file.zip |
For example:
1 |
$ sudo unzip -q nextcloud-18.0.3.zip |
Overwrite an existing unzipped file
If you already have an unzipped file and you messed or deleted a few files, you can unzip the zipped file again to overwrite the existing unzipped file. Usually, you will be prompted whether you want to overwrite the current file, whether you’d like to skip overwriting it or whether you want to replace all the files.
To suppress such prompt, use the -o flag to proceed with
1 |
$ unzip -o file.zip |
Unzip a file in Linux without overwriting
There are scenarios that you may want to skip overwriting a file(s). Let’s assume you have an unzipped file containing several files. You make some changes to some of the extracted files, however, something goes wrong and you lose some of the files due to accidental deletion. In this case, you may be compelled to unzip the file again, but this time, retain the changes you made to some of the files.
To avoid overwriting the changes, use the -n flag as shown:
1 |
$ unzip -n file.zip |
The outcome will be the omission of the extraction of preexisting files. You will get your deleted files back but the changes you made to some of the files will remain intact.
List contents of a zipped file
You can always have a peek to verify the contents of a zipped file. This is achieved using the -l option as shown:
1 |
$ unzip -l file.zip |
For example:
1 |
$ sudo unzip -l nextcloud-18.0.3.zip |
On your terminal screen, you will have the contents of the zipped file listed as shown:
Unzip tarball files
Thus far, we have looked at how you can extract or unzip files with a .zip file extension. In this section, we look at how you can extract tarball files.
A tarball file is an archive file that has been compressed using the tar command. TAR, short for tape archive. We already have an article on how you can use tar command in basic Linux commands. Tarball files usually have a .tar.tgz, .tar.bz2, tar.xz or tar.gz
To unzip a tar xz file, tar tgz file or tar bz2 file, use the syntax below
1 |
$ tar -xvf tarball |
Let’s take a look at the options used:
x – This instructs the tar command to extract the file
v – This prints verbose output on the screen
f – This invokes the file name
For example, to unzip to extract a file latest.tar.gz
, execute the command:
1 |
$ tar -xvf latest.tar.gz |
How to zip files/directories in Linux
In this section, we will see how we can zip a file or a directory. To create a zip file, we use the zip command-line utility. The basic syntax is shown below
1 |
$ zip -r name_of_archive.zip file or directoryname |
The zip utility comes installed in most major and popular Linux distros. While that may be the case, should you encounter a system without the zip tool installed, here’s how you can install the command-line tool.
How to install zip command in Linux
In this section, we are going to look at how to install unzip command in various Linux distributions.
Install zip in Ubuntu/Debian distributions
For Debian systems, you can install zip by running the command:
1 |
$ sudo apt install zip |
Install zip in RHEL / CentOS
For Redhat-based Linux systems, install zip by running the command:
1 |
$ sudo yum install zip |
Install zip in Fedora
Again, for Fedora, use the dnf package manager to install zip as shown below.
1 |
$ sudo dnf install zip |
Install zip in ArchLinux / Manjaro
For Arch-based systems like Manjaro, Install zip by executing the command:
1 |
$ sudo pacman -S zip |
Install zip in OpenSUSE
To install unzip in OpenSUSE systems, run the command:
1 |
$ sudo zypper install zip |
Let’s now shift gears and check out a few examples of how you can unzip files in Linux.
Zip files in Linux
Quite notably, zip is the opposite of unzip command. This involves creating a tarball file or archive. Zipping a file on the terminal is quite easy and takes the following syntax:
1 |
$ zip name_of_archive.zip filename1 |
The above command zips a single file into a zipped archive. Alternatively, you can zip multiple files into one zip archive as shown:
1 |
$ zip name_of_archive.zip filename1 filename3 filename3 ... |
Zip directories in Linux
Often times, you will be required to zip a directory to save on the hard disk space on your system. To achieve this, use the -r option as shown.
1 |
$ zip -r name_of_archive.zip directoryname |
On top of that, you can include multiple directories and files into the zip archive as shown:
1 |
$ zip -r name_of_archive.zip directory1 directory2 filename1 filename2 |
And this concludes our tutorial on how you can unzip a zip file on a Linux system. Your feedback is most welcome.
-
DevOps55 years ago
Saltstack Tutorial for beginners [2023]
-
DevOps55 years ago
How to build a Docker cron job Container easily [2023]
-
Linux55 years ago
mail Command in Linux/Unix with 10+ Examples [2023]
-
DevOps55 years ago
Docker ADD vs COPY vs VOLUME – [2023]
-
DevOps55 years ago
How to setup Pritunl VPN on AWS to Access Servers
-
Linux55 years ago
Grep Command In Unix/Linux with 25+ Examples [2023]
-
Linux55 years ago
How To setup Django with Postgres, Nginx, and Gunicorn on Ubuntu 20.04
-
Linux55 years ago
Find command in Unix/Linux with 30+ Examples [2023]