How to Unzip a Zip File in Linux Command Line

Last updated: May 24, 2022

Zip is a popular command-line tool to compress files. It allows us to create Zip archives. By default, it uses a lossless data compression technique.

Once the compressed zip file is placed in the target directory, it needs to be extracted. In this tutorial, we learn how to unzip a zip file in Linux command line using unzip command.

Prerequisites

  • Unzip package installed
  • If the package is not installed required a user account with sudo access

Install unzip Package in Linux

The new versions of Linux Distributions are already installed with unzip package. If not installed you may see "sudo: unzip: command not found" error when you try unzip command. You can easily install unzip package using the package manager.

Ubuntu / Debian

To install unzip on Ubuntu and Debian use the apt command:

sudo apt install unzip

Fedora / Redhat / AlmaLinux / Rocky Linux

On RPM-based Linux Distributions such as Fedora, Redhat, AlmaLinux, and Rocky Linux you can use the DNF package manager.

To install the unzip package using DNF, type:

sudo dnf install unzip

You may also use yum to install unzip command:

sudo yum install unzip

unzip Command in Linux

The unzip command is mainly used for unzipping - extracting files from a specified zip archive file. The archive file will have a .zip file extension. You may also use unzip command to list and test the zip files. By default unzip command extracts into the current directory.

The basic syntax for unzip command:

unzip options [-d dir] [-x pattern] [-P password] file.zip

Unzip a ZIP File with the unzip

Unzipping your compressed zipped file is simple and straightforward.

For example to unzip a zip file named archived_file.zip, type:

unzip archived_file.zip

When unzip is used without any options, it extracts to the current directory. You can use ls command to verify it.

unzip a file named archived_file.zip

Make sure you have write permissions on the current folder where you will extract zip files to.

By default, unzip linux will print the names and summary of the files it has extracted. Using the -q option will unzip all the files in a quiet mode:

unzip -q archived_file.zip
unzip without output using -q option

Extracting Files to a another directory

To unzip a ZIP file to a different directory, we use the -d option followed by the absolute path:

unzip archived_file.zip -d /path/to/directory
unzip extract to a directory

Exclude Files

When unzipping you can exclude specific files and subdirectories. That means those files or directories will be kept as it is in the archive file, and won't be extracted.

Use -x option to exclude files or directories while extracting.

For example to exclude files named filename1 and dual from being extracted, type:

unzip archived_file.zip -x filename1 dual
unzip exclude files

Overwrite Existing Files

Unzipping twice will prompt for overwriting existing files. This scenario happens in cases where you have deleted some extracted files and to fix that you have to extract files again.

When run unzip again, it will prompt to replace:

unzip archived_file.zip
unzip overwrite
  • [y]es: overwrite current file
  • [n]o: don’t overwrite current file
  • [A]ll:  overwrite all of the files
  • [N]one: overwrite none of the files
  • [r]ename: Extract current file but give it a new name and prompt for a new name.

For unzip to overwrite the files without raising any prompt, use the -o option. This perform force unzip overwriting any existing files.

unzip -o archived_file.zip
unzip overwrite files without prompting

Without Overwriting Existing Files

For unzip to extract only files that are not present in the target directory use -n option.

Never overwrite a scenario that could happen when you have to keep all the extracted files with changes and restore the deleted files.

unzip -n archived_file.zip
unzip without overwrite in linux

List Contents of ZIP Archive

Use -l option to list the contents of the ZIP archive file. This option helps to see what's inside the zip file before extracting it.

For example to list the contents of the zipped archived file named archived_file.zip, type:

unzip -l archived_file.zip
unzip list files

You can use unzip pipe through less to get the big list of files at a time.

unzip -l archived_file.zip | less

Unzip multiple ZIP files

To perform unzip multiple files which are in .zip extension, you may need to use regular expression.

The following command unzip all .zip files in the current directory:

unzip '*.zip'

Password protected zip file

Use -p option to unzip a password protected zip file. The -p option should be followed by the password which was used to zip the file.

For example:

unzip -P mypassword archived_file.zip
unzip password protected zip file

A rather more secure option would be to extract the file without providing the password. This way command will prompt for the password.

unzip archived_file.zip

Verify ZIP Archive Integrity

Using unzip command you can test the archive files using the -t option. This helps to find any corrupt files inside the archive.

Remeber this process may take a longer time to complete as it performs cyclic redundancy checks.

unzip -t archived_file.zip
unzip test integrity

Conclusion

In this tutorial, we learned how to unzip files from Linux command line. Commonly unzip command is used to extract compressed ZIP archives and list zip file contents.

For more information browse to the man unzip page. Thanks for reading, please provide your feedback and suggestions in the below comment section.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment