How to Unzip Files in Linux

Updated on

5 min read

Unzip Files in Linux

Zip is the most widely used archive file format that supports lossless data compression. A zip file is a data container containing one or more compressed files or directories, which can later be extracted to restore the original files.

Zip also offers a range of advanced features. You can password-protect your ZIP archives to keep your data safe or split large files into smaller parts to make them more manageable.

In this article, we will explain how to use the unzip command to unzip files in Linux systems.

Installing unzip

The unzip utility is not installed by default in most Linux distributions. However, you can easily install it using your distribution’s package manager.

Install unzip on Ubuntu and Debian

sudo apt install unzip

Install unzip on CentOS and Fedora

sudo yum install unzip

How to Unzip a ZIP file

In its simplest form, when used without any options, the unzip command extracts all files from the specified zip archive to the current directory.

For instance, let’s say you downloaded the WordPress installation zip file. To unzip this file to the current directory, you’d simply run the following command:

unzip latest.zip

Zip files do not store ownership information. The extracted files are owned by the user that runs the command.

You must have write permissions on the directory where you are extracting the ZIP archive.

Suppress the Output of the unzip Command

By default, unzip prints the names of all the files it’s extracting, and it will also give you a summary once the extraction process is complete.

To suppress the printing of messages, use the -q option:

unzip -q filename.zip

Unzip a ZIP File to a Different Directory

If you want to extract the contents of a zip file to a directory other than the present working directory, you can use the -d option. This allows you to specify the path of the destination directory where you want to extract the files.

unzip filename.zip -d /path/to/directory

For example, to unzip the WordPress archive latest.zip to the /var/www/ directory, you’d use the following command:

sudo unzip latest.zip -d /var/www

In the command above, we are using sudo because the user we are logged in as typically does not have write permissions to the /var/www directory. When zip files are decompressed using sudo, the extracted files and directories are owned by the user root.

Unzip a Password Protected ZIP file

If you have a zip file that is protected with a password and you want to extract its contents, you can use the unzip command with the -P option. This option allows you to specify the password required to open the file. Once you enter the correct password, the contents of the zip file will be extracted to the current directory.

unzip -P PasswOrd filename.zip

Typing a password on the command line is insecure and should be avoided. A more secure option is to run the command without using the -P option.

If the zip file is password protected, unzip will prompt you to enter the password:

unzip filename.zip

Type the password and press enter.

archive:  filename.zip
[filename.zip] file.txt password: 

unzip will use the same password for all encrypted files in the archive as long as it is correct.

Exclude Files when Unzipping a ZIP File

When you want to exclude specific files or directories from being extracted from an archive, use the -x option, followed by a list of the archive files you want to exclude from the extraction process, separated by spaces.

unzip filename.zip -x file1-to-exclude file2-to-exclude

In the following example, we are extracting all files and directories from the zip archive except the .git directory:

unzip filename.zip -x "*.git/*"

Overwrite Existing Files

Let’s imagine a scenario where you have previously extracted the contents of a zip file, and now you are attempting to execute the same command again. In other words, you are trying to unzip the same file again.

unzip latest.zip

By default, unzip will ask you whether you like to overwrite only the current file, overwrite all files, skip extraction of the current file, skip extraction of all files, or rename the current file.

Archive:  latest.zip
replace wordpress/xmlrpc.php? [y]es, [n]o, [A]ll, [N]one, [r]ename:

If you want to overwrite existing files without being prompted, use the -o option:

unzip -o filename.zip

Use this option with caution. If you previously made any changes to the files, the changes are lost.

Unzip a ZIP File Without Overwriting Existing Files

Suppose you have unzipped a zip file and modified some of its files. However, you mistakenly deleted a few files that you now need to recover from the zip archive. What you want to do now is to restore the deleted files from the zip archive while keeping the changes you have made.

In this scenario, use the -n option, which instructs unzip to skip the extraction of a file if it already exists in the target directory:

unzip -n filename.zip

Unzip Multiple ZIP Files

You can use regular expressions to match multiple archives.

For instance, if you have multiple zip files in your current working directory , you can unzip all files using only one command:

unzip '*.zip'

Note the single quotes around *.zip. If you forget to quote the argument, the shell will expand the wildcard character, and you will get an error.

List the Contents of a Zip File

To list the contents of a zip file, use the -l option:

unzip -l filename.zip

In the example below, we are listing all WordPress installation files:

unzip -l latest.zip

The output will look like this:

Archive:  latest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2023-08-02 22:39   wordpress/
     3065  2021-08-31 18:31   wordpress/xmlrpc.php
      364  2021-12-19 12:20   wordpress/wp-blog-header.php
     7415  2023-03-18 17:13   wordpress/readme.html
...
...
    21323  2023-03-09 01:15   wordpress/wp-admin/themes.php
     8353  2022-09-10 18:20   wordpress/wp-admin/options-reading.php
     4620  2021-10-24 00:12   wordpress/wp-trackback.php
     1889  2023-05-03 00:11   wordpress/wp-comments-post.php
---------                     -------
 27271400                     1648 files

Conclusion

unzip is a command line tool that allows you to list, test, and extract compressed zip archives.

To create a ZIP archive on a Linux system, you can use the zip command .

Feel free to leave a comment if you have any questions.