In this article, we’ll learn how to unzip multiple files in Linux. Zip files are compressed files that provide an ideal way of transferring data across the internet.
These files can be easily extracted using the command but what if you have to unzip thousands of files? Manually unzipping each file will take too much time.
Thankfully we can make use of power shell scripting to automate this job for us. So let’s take a look at how we can unzip multiple files at once and since we are using shell scripting, this method will be applicable to all distributions.
Easiest way to bulk unzip all zip files in a folder
Before we proceed to the more complex, customizable methods, here’s a quick one:
unzip *.zip
OR
unzip *.zip -d /path/to/destination/folder
And that’s it. This command will however unzip every single file that is present within the folder. You can use some more customizable BASH commands like for, if-else, etc to decide which files you want to unzip.
Let’s take a look at bulk unzipping using the for loop.
Bulk Unzipping multiple files in the same folder
If you have a folder with multiple files to be unzipped, you can execute the following code inside that folder to unzip them all.
for z in *.zip; do unzip "$z"; done
If you have any experience with programming, you will recognize that this is just a simple for-loop. The for-loop, in this case, iterates over the list of files that have a “.zip” extension in the end and runs the unzip
command on them. This code will extract ALL files with a .zip extension.
Unzipping all files in all subfolders of a directory
If you have a folder with multiple subfolders all containing zip files that need to be unzipped, you can run this command to unzip them all.
for k in $(find ./ -type f -name '*.zip') ; do unzip $k ; done
This command finds all files with *zip extension under a certain directory and then unzips them. All the files are extracted in the current working directory.
If you don’t have the unzip
software, you will get an error while running the code. Here is how you install unzip on your system:
- Debian / Ubuntu / Mint / Debian derivates
sudo apt install unzip
- Fedora / RedHat / CentOS
sudo dnf install zip
- Arch / Manjaro / Arch-based distributions
sudo pacman -S unzip
- OpenSuse
sudo zypper install unzip
Additional notes
The *.zip
is called regex (Regular Expressions). In this case, the regex tells the system to select files that have anything at the start but necessarily have a “.zip” at the end. (ex. a.zip, b.zip, je3j3je9e.zip)
If you don’t want to extract all files in the folder, you can manipulate the regex to select the files you want. Regex is a very powerful language and you can learn more about it here.
Conclusion
This is just one of the ways shell-scripting can help you in automating daily tasks. You can modify this program to suit your needs. Good luck unzipping!