Unzip file to current directory Linux

Compressing files is an excellent way to save storage and bandwidth while transferring files swiftly. There are numerous file compression formats like ZIP, RAR, and TAR. Undoubtedly, ZIP is the most commonly used compression format, especially within the Windows and Linux ecosystems.

Since this guide is for Linux-based OSes only, you can use unzip, a popular command-line tool for zipping and unzipping files within your system.

Installing unzip on Linux

The unzip command is necessary to perform zipping and unzipping tasks in Linux. You can install unzip on almost every distro using the default package manager.

On Ubuntu and Debian:

sudo apt install unzip

To install unzip on Fedora, CentOS, or other RHEL-based distros:

sudo yum install unzip

On Arch Linux and its derivatives:

sudo pacman -S unzip

Unzipping Files Using unzip

The unzipping process extracts all the files present inside the ZIP file. These files are extracted to the current directory by default, but you can change this by unzipping the file to a different directory.

Use the following command to unzip files in Linux:

unzip filename.zip

The aforementioned command prints the name of all the extracted files in the output. You can mute the output of the command using the -q flag:

unzip -q filename.zip

The -q essentially stands for quiet, hiding the output of the unzip command.

Unzip a ZIP File to a Different Directory

You can extract the ZIP file to a separate folder to keep the current directory prim and proper. This also arranges the extracted files and ensures they don't get mixed with other files present in the current directory.

To extract files to a different directory, use the -d flag with the command. The -d flag stands for Directory and takes a relative or absolute path as the argument.

unzip filename.zip -d /directory/location

Unzip a Password Protected ZIP File

Protecting ZIP files using a password is a common practice to ensure security and maintain confidentiality. You can unzip a password-protected ZIP archive using the following command:

unzip -P your_password filename.zip

...where your_password is the password for the archive.

To unzip a file without using the command line, you can use the traditional Extract method provided in almost every mainstream Linux desktop environment.

Open the folder containing the archive, double-click the file, and click on the Extract button at the very top left of the window.

Unzip file to current directory Linux

Extract ZIP file with unzip command

Using the simple unzip command, you can extract all files from the zip archive into the current zip file directory as follows:

For example, we have downloaded a zip file in the ‘Downloads’ with the name ‘testfile.zip’. So, first, navigate into Downloads directory and then we have extracted the zip file using the following command:

$ cd Downloads
$ unzip testfile.zip

Unzip file to current directory Linux

Unzip the file to another directory

Using -d switch with unzip command, you can extract the file to a different location instead of the current directory. The basic syntax is as follows:

$ unzip filename.zip -d /directory-path

For example, we want to extract a zip file on the desktop instead of the current Downloads. So, we used the following command to do this:

$ sudo unzip testfile.zip -d /home/kbuzdar/Desktop

Unzip file to current directory Linux

As you can see in the following image the folder has been extracted on my desktop. You should have read and write permissions to extract a ZIP file to different directories.

Unzip file to current directory Linux

Extract ZIP files with suppressing output

When you extracted a zip file then it first prints the all files name during extraction and also displays a summary on completion. Using ‘-q’ switch with unzip command, you can avoid printing these messages on the terminal as follows:

For example, we have extracted a ‘testfile.zip’ using ‘-q’ switch. You can see the zip file is extracted without printing files names.

Unzip file to current directory Linux

Exclude files from extracting a ZIP file

You can exclude directories and files while unzipping a zip file. Use ‘-x’ switch with unzip command and excluded files name separated with space as follows:

$ unzip file-name.zip -x name-exclude1 name-exclude2

For example, we want to exclude ‘wp-content’ and ‘wp-admin’ directories on extraction as follows:

$ unzip testfile.zip -x "*wp-content*" "*wp-admin*"

Unzip file to current directory Linux

Overwrite the existing unzipped files

If you have already unzipped file and you will again run the command as follows:

In this case, it will ask you to overwrite the existing unzipped file which is displayed in the following output:

Unzip file to current directory Linux

To overwrite the existing files without generating prompt, you will use ‘-o’ option with unzip command as follows:

Please use this command carefully because due to any mistake you can lose your original data.

Unzip file to current directory Linux

List contents of the zip file

The ‘-l’ option with unzip command is used to list the contents of a zip file as follows:

In the following example, we have listed the contents of ‘testfile.zip’.

Unzip file to current directory Linux

If you don’t want to overwrite the existing files or accidentally delete some extracted files. Then, you can use the ‘-n’ option with unzip command that will forcefully skip extracting those files that are already extracted or exist.

Unzip file to current directory Linux

Unzip multiple files

Using regular expressions, you can unzip multiple matched archive files in the current directory. To unzip multiple files, you will use the following command on the terminal:

In the following image, you see that it will extract all zip files from the current working directory.

Unzip file to current directory Linux

How to unzip password protected files?

You can also unzip the password-protected zip files using the unzip command as follows:

$ unzip -P Password file-name.zip

To open password-protected files using the command line is not secure. So, it is better to avoid it.

In this article, you learned the uses of unzip command that is very helpful to list and extract the ZIP archives. I hope this article would be useful for you in the future.

About the author

Unzip file to current directory Linux

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.