How to Unzip File on Linux
The zip file format is a versatile and convenient way to share files, and its popularity is a testament to its effectiveness. It combines and compresses multiple files while maintaining the original quality. The zip files come with a .zip extension and commonly shared file formats on the Internet.
If you have just downloaded a zip file on Linux, then you can use the unzip utility to unzip it.
In this guide, I will discuss different methods to unzip zip files in the terminal.
Installing unzip on Linux
To extract the zip archive file on Linux, the unzip command line tool is used. In many latest versions of Linux distributions, including Ubuntu 22.04, Debian 12, and its derivatives, the unzip utility comes preinstalled by default. However, you may not find this utility installed on all Linux distributions. To install it on various Linux distributions, use the command mentioned below.
To install unzip on Ubuntu and Debian and its derivatives.
sudo apt install unzip
For CentOS, RHEL, and their derivatives.
sudo yum install unzip
For Manjaro and Arch Linux.
sudo pacman -S unzip
And for OpenSUSE.
sudo zypper install unzip
Unzip a File
To unzip a file, use the unzip command with the file name. For example, to extract file.zip, use the command mentioned below:
unzip file.zip
Unzip Multiple Files
With the unzip utility, you can use wildcard expressions such as asterisk (*) and question mark (?) which helps in extracting multiple zip files. The * matches the sequence of zero or more characters, while the ? matches on one character. The square brackets ([]) are also used to mention the range of characters.
For example, to unzip all the zip files in the current working directory, use the unzip command and .zip extension postfixed by *.
unzip '*.zip'
Do not forget to use single quotes, otherwise, the shell will expand the wildcard.
To unzip multiple archive files, the combination of unzip and find commands can also be utilized. For example, to unzip all the files in the current working directory, use the find command with the -exec option to process the files by unzip command.
find . -type f -name '*.zip' -exec unzip '{}' \;
In the above command, the dot signifies the current working directory, which can be replaced with any other directory.
Unzip a File to a Different Directory
With the unzip command, the zip file can be extracted to a different directory using the -d option. For example, to unzip the file.zip to a directory dir, use the unzip command with the file name and directory path with -d option.
unzip file.zip -d /home/user/dir
You may need sudo to unzip a file to a directory that requires extended privileges.
Listing the Content of the Zip File
To extract a specified file from the zipped file, it is important to know what that archive file contains.
To list the content of a zip file, the -l option is used with the unzip command. For instance, to list the content of file.zip, use the unzip command with the -l option and file name.
unzip -l file.zip
Unzip a File Excluding Specific Files
To unzip a file excluding specific files, the -x option is used with the unzip command. For example, to keep out files from the file.zip file, use the unzip command with file names that you want to exclude followed by the -x option.
unzip file.zip -x file1.txt file2.txt
Similarly, to exclude all the HTML files from the zip file, use the unzip command with the zip file name and .html extension with the wildcard.
unzip file.zip -x '*.html'
To unzip all the files excluding a directory and its content, use the unzip command with zip file name and directory path.
unzip file.zip -x '*/dir1/*'
Unzip a Password Protected File
Upon extracting the locked zip file on Linux with the unzip command, it prompts you to type the password.
However, the -P option can also be used to specify the password while extracting.
unzip -P [password] file.zip
However, it is not the most secure way to unzip a file.
Unzip a File Overwriting the Existing Files
If the extraction process is disturbed, then to resume it you need to re-extract the file, overwriting the existing files. By default, the unzip utility asks to overwrite the existing files while simply unzipping it. However, it can be tedious to respond to prompts for each existing file. To bypass these prompts, the -o option is used with the unzip command.
unzip -o file.zip
Unzip a File with the Updated Files
If you want to avoid overwriting already existing files, then you can use the -f or -u option with the unzip command. The -f stands for freshen and -u for update. For example, if one or more files are already present in the current directory, the unzip utility with -u or -f options will only extract the files that are not present. But if the files in the zip file are updated or modified, then they will be overwritten.
Let’s understand it with an example. The file.zip file contains three text files; file1, file2, and file3. I have already extracted file3.txt. Now, if I want to extract the remaining files, I will use the -u option with the file name.
unzip -u file.zip
It will extract the remaining files, only if the file3.txt is not modified. If the file3.txt is modified, then it will replace the older file.
Unzip a File Discarding Directories
If you want to extract the files, discarding the subdirectories and files in them, the -j option is used with the unzip command. The -j stands for junk paths. For example, if you have a zip file with two directories dir1 and dir2, and files in them, then use the -j option to extract files from the main directory only ignoring the subdirectories.
unzip -j file.zip
Unzip a File by Suppressing the Output
By default, extracting a zip file using the unzip command prints the comments on the console. But if you are using the unzip command in a script then you can suppress the output by using the -q option which signifies quiet.
unzip -q file.zip
Testing a Zip File
The unzip command can also be used to test the integrity of the zip file using the -t option. It extracts the files on the memory and performs the Cyclic Redundancy Check (CRC), and checksum comparison with the original CRC and checksum.
unzip -t file.zip
Conclusion
The unzip utility is used to extract the zip archive files on Linux. In many latest versions of Linux distributions, the unzip utility comes preinstalled by default. However, you may need to install it if it is not present. It offers many options to unzip the files. In this guide, I covered various methods to unzip a file using the unzip command, including other features that it offers; such as testing a file, excluding specific files, and removing spaces from the extracted files and directories.