How to Unzip Files in Linux
ZIP is a popular lossless compression method used to contain files and directories. It is especially useful to ZIP files before transferring them since a lot of files can be sent in one stream rather than starting a new stream for each one, speeding up downloads dramatically.
Today we will be exploring how to unzip files using the command line in Linux. On top of the basic use of unzip in Linux, we will also look at different ways you can control how a ZIP archive is extracted.
Most Linux distributions don't have Unzip installed by default. To install it on Ubuntu and Debian type the following command and press enter:
sudo apt install unzip
for CentOs use:
sudo yum install unzip
How to Extract All Zipped Files
The most basic way to use the unzip command is by passing the ZIP file as the second argument in the command.
PRO TIP press tab when typing the file name and it will auto-complete. This also works on directories.
The contents of the ZIP archive will be put in the directory you are currently in. If you are not the owner of the directory you will need to run unzip in sudo mode. The newly created files will be owned by whichever user executes the unzip command.
Don't Show the Unzip Process
During the unzip process you will see that the names of the extracted files are printed in a huge list. To hide this pass
-q as the second argument.
unzip -q archive.zip
Unzip to a Different Directory
If you want to unzip files to a specific directory you can do this by passing
-d followed by the path of the directory.
unzip archive.zip -d /var/www/example/
Selectively Excluding Files from Unzipping
With unzip you can provide in a list of file names after the
-x flag to exclude them from the extraction process.
unzip archive.zip -x file1.txt another_file.mp4
Password Protected ZIP Files
Some ZIP are password protected, if this is the case, proceed like you would with the previous steps and you will be prompted to enter the password.
You can pass-in a password using the
-P but typing a plaintext password into your terminal is not a good idea for obvious security reasons.
unzip -p pa$$word archive.zip
Unzip Without Overwriting Existing Files
By default, the unzip command in Linux will automatically overwrite files with the same name as the ones in your ZIP archive. To skip and leave the original files pass in the
unzip -n archive.zip
Show Contents of a ZIP File
If you want to see what is inside of a ZIP file you can use the list flag, which will display all the files inside of the ZIP archive and some details about them
unzip -l archive.zip
Unzip Multiple ZIP Files
If you have a lot of ZIP files that need extracting you can do this using a wildcard name in your unzip command.
The above command will take all the zip files in the current working directory and extract them in the same way as doing them one-by-one manually.