How to Unzip Files in Linux – Extract .zip File in Ubuntu, Debian, CentOS, etc

how to unzip files in

Learn How to Unzip Files in Linux with Command Line using unzip – the most widely used archive file format that supports lossless data compression. ZIP files are data containers that hold one or more compressed files or directories. Follow this step-by-step tutorial to easily extract the contents of ZIP files on Linux systems 2024.

Installing unzip

If you’re using a Linux distribution, it’s important to note that the unzip utility may not come pre-installed. However, the good news is that you can effortlessly install it through your distribution package manager.

How to install unzip on Ubuntu and Debian

Install unzip on Ubuntu and Debian Linux by the following command line

sudo apt install unzip

How to install unzip on CentOS and Fedora

Install unzip on CentOS and Fedora Linux by the following command line

sudo yum install unzip

How to unzip a zip file on Linux

To unzip a compressed zip file, you need to use the unzip command. It will unzip selected zip files. If you want to unzip the file_name.zip file, then use

unzip file_name.zip

As you’re working with ZIP files on Linux, keep in mind that it doesn’t support ownership information in the same way that Linux does. When you extract files from a ZIP archive, they will be owned by the user who executed the command. Additionally, it’s crucial to ensure that you have to write permissions on the directory where you plan to extract the ZIP archive.

Suppress the Output of the unzip Command

If you want to suppress the printing of file names and summary messages when extracting files with unzip, you can use the -q switch. By default, unzip will print the names of all extracted files and a summary once the extraction is complete. Opting for the -q switch can help avoid cluttered output and streamline the extraction process.

unzip -q filename.zip

How to unzip a ZIP File to a Different Directory

If you want to extract a ZIP file to a directory other than your current one, simply use the -d option.

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

If you want to extract the file_name.zip archive into the /var/www/ directory, use this command as an example.

sudo unzip file_name.zip -d /var/www

To decompress ZIP files in the /var/www directory, the sudo command is required as the logged-in user often lacks the necessary write permissions. Using sudo ensures that extracted files and directories are owned by the root user. Check out this helpful guide on how to list users on a Linux system.

How to Unzip a Password-Protected ZIP file

To effectively unzip a password-protected file, utilize the “unzip” command with the -P option and input the corresponding password.

unzip -P Password file_name.zip

To ensure optimal security, it is recommended to avoid typing passwords on the command line. Instead, consider extracting files using a more secure approach that doesn’t require entering passwords. If the ZIP file is encrypted, you can still extract it by using the unzip command, which will prompt you to enter the password. By following these best practices, you can enhance your data security and protect your files from potential threats.

unzip file_name.zip
Output

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

If the correct password is provided, unzip command will use it uniformly across all encrypted files.

How to Exclude Files when Unzipping a ZIP File

If you need to prevent certain files or directories from being extracted, utilize the -x option and provide a list of archive files you want to exclude from extraction, separated by spaces. This method will help you streamline your website’s content and ensure that search engines can easily find and index the information you want to prioritize.

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

If you want to extract all files and directories from a ZIP archive but exclude the .git directory, check out this example. We’ll show you how to easily extract everything except the .git directory.

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

How to overwrite Existing Files

unzip file_name.zip

When unzipping files, the unzip utility will prompt you to choose from several options, including overwriting the current file, overwriting all files, skipping extraction of the current file, skipping extraction of all files, or renaming the current file.

Output

Archive: file_name.zip
replace folder/index.php? [y]es, [n]o, [A]ll, [N]one, [r]ename:

To overwrite existing files without any prompt, you can utilize the -o option.

unzip -o filename.zip

Exercise caution while utilizing this option. Any modifications made to the files will result in the loss of said alterations.

How to Unzip a ZIP File Without Overwriting Existing Files

If you’ve made changes to some files in a previously unzipped ZIP file but accidentally deleted a few, you may be wondering how to restore them without losing your changes. The solution is to use the -n option with the unzip command, which will force the program to skip the extraction of any files that already exist. With this method, you can easily restore your deleted files while preserving your changes.

unzip -n file_name.zip

How to Unzip Multiple ZIP Files

To match multiple archives, regular expressions can be used effectively. For instance, if there are several ZIP files in the current working directory, you can extract all of them simultaneously by executing a single command.

unzip '*.zip'

When using wildcards in the shell, it’s important to remember to enclose the filename pattern in single quotes. For example, when using the *.zip wildcard, enclose it in single quotes to prevent the shell from expanding it and causing errors. So, always remember to use single quotes to avoid errors when using wildcards in shell commands.

How to List the Contents of a Zip File

To display the contents of a ZIP file, include the -l option in your command.

This version includes the keyword “ZIP file” as well as the action verb “display” which are both relevant to the topic. The use of the word “command” implies the use of a computer or device, which can help improve search rankings for queries related to software or tech.

unzip -l file_name.zip

Looking for a comprehensive list of all files in an example.zip file? Check out the example below where we have compiled a complete index of the example.zip file.

unzip -l example.zip

The output will look like the following one:

Output

Archive: example.zip
Length Date Time Name
——— ———- —– —-
0 2023-02-02 21:10 example/
12987 2023-02-02 21:10 example/index.html
321 2023-02-02 20:10 example/readme.html
3432 2023-02-02 21:13 example/index2.php
1234 2023-02-02 21:10 example/blog.html
——— ——-
27271400 1648 files

Here is a video tutorial on how to unzip files in Linux.

Conclusion

If you’re looking to list, test, or extract compressed ZIP archives, unzip is a handy utility to have. However, when it comes to creating a ZIP archive on a Linux system, you’ll need to rely on the zip command. Keep reading to learn more about using unzip and zip to manage your files efficiently.
Hope this tutorial helped you to extract your zip files in Linux. If you have queries, drop them in the comment box.

Nico Lewis

Nico Lewis

Hi, this is Nico Lewis, a tech professional and a core member of TrioTeam who has been writing solutions to technical problems, how to tutorials, new technology, tools and websites, and so on for TrioGuide.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo