If you’ve ever had to extract a large number of zip files, you know it can be a time-consuming and tedious process. But on Linux and other UNIX-like distributions, there’s a much easier way to do it. In this article, we’ll show you how to unzip multiple zips files using just a single line of code. So let’s get started!
Trying the first command that comes to mind
Let’s say the files are named 1.zip, 2.zip, 3.zip, and so on. The first command that comes to our mind to extract these zipped files is:
unzip *.zip
However, when you run the command, the terminal gives you the following output :
Archive: 1.zip
caution: filename not matched: 2.zip
caution: filename not matched: 3.zip
Well, this was not how it’s supposed to go. What could be the problem? This will help us understand a bit more about how the Terminal works on Linux.
So what is the issue?
When you use a wildcard (*) in any command, the shell will expand that command and pass the results to the program, rather than the program handling the expansion itself. That means that our previous command was actually expanded to the following before being executed :
unzip 1.zip 2.zip 3.zip
This might appear normal to you, as other commands like mkdir will make three separate folders when you type it in this exact manner. But, Unzip works in a different way than this. Unzip will interpret the above command as extract 1.zip and then inside 1.zip, extract another zipped file named 2.zip.. and so on for the 3rd zip file as well. And we do not want that, and this is the reason it throws out the error.
What is the correct command, then?
The simple solution is to prevent the shell from expanding the wildcard, and then the unzip command will automatically expand it for us. So the correct command would look something like this :
unzip '*.zip'
And that’s what we had to do! The unzip command works perfectly now.
Summary
I hope you learned something new about how the shell works in the terminal. The correct way to unzip multiple files is just to add the (‘) before and after the wildcard. I hope you remember this neat little trick when you do some shell or python scripting.