Abstract: use the -o option. This perform force unzip overwriting any existing files. unzip -o archived_file.zip Without Overwriting Existing Files For unzip to
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
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:
Fedora / Redhat / AlmaLinux / Rocky Linux
sudo apt install unzip
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:
unzip Command in Linux
sudo yum install unzip
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 a ZIP File with the unzip
unzip options [-d dir] [-x pattern] [-P password] file.zip
Unzipping your compressed zipped file is simple and straightforward.
For example to unzip a zip file named archived_file.zip, type:
When unzip is used without any options, it extracts to the current directory. You can use ls command to verify it.
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:
Extracting Files to a another directory
unzip -q archived_file.zip
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
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:
Overwrite Existing Files
unzip archived_file.zip -x filename1 dual
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:
- [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.
Without Overwriting Existing Files
unzip -o archived_file.zip
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.
List Contents of ZIP Archive
unzip -n archived_file.zip
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
You can use unzip pipe through less to get the big list of files at a time.
Unzip multiple ZIP files
unzip -l archived_file.zip | less
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:
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.
unzip -P mypassword archived_file.zip
A rather more secure option would be to extract the file without providing the password. This way command will prompt for the password.
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
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.