Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
While coding, we download several data in ZIP format. We need to extract these files to use them. If there are multiple ZIP files, extract each one separately is a tedious process. To ease this in Linux, we have presented over 5 methods to unzip multiple ZIP files together at once.
We have covered different cases such as unzipping in new directory and unzipping all zip files in nested directories.
Table of contents:
- Unzip ZIP files in the current directory [2 commands]
- Unzip ZIP files in Nested directories
- Bash scripts to unzip in new directory
In short, the command to unzip all ZIP files in current directory is:
unzip \*.zip
OR
unzip "*.zip"
Unzip ZIP files in the current directory [2 commands]
If there are multiple ZIP files in the present working directory at the same level, we can extract all ZIP files at once using either of the following 2 commands:
unzip \*.zip
OR
unzip "*.zip"
There are two issues with this approach:
- This does not unzip ZIP files that are in nested directory.
- Everything will be extracted in the present working directory so if there is a large number of files in a ZIP with no directory inside, all files will be mixed with other ZIP files.
- Solution is to unzip each ZIP file in a new directory
We fixed both these problems in the next few commands.
Unzip ZIP files in Nested directories
If you would like to extract all ZIP files even in nested directories, this command will help:
find . -name "*.zip" -exec unzip {} \;
The find command finds as files with extension zip which is passed one by one to the unzip command. Hence, all ZIP files are extracted using this command.
Bash scripts to unzip in new directory
There may be a case when you want to unzip each ZIP file into a new directory with directory name same as the ZIP filename. This is not possible with a single command but we can develop a small working BASH script to do the task.
This will also give you practice in developing small bash utilities. Learn the basics of shell scripting.
See this script:
#!/bin/sh
for i in *.zip; do
newdir="${i:0:-4}" && mkdir "$newdir"
unzip "$i" -d "$newdir"
done
The key idea is:
- It loops through all ZIP files in current directory (not nested directory)
- It creates a new directory with the same name as the ZIP filename.
- The current ZIP file is unzipped and the output directory is specified using the d option.
unzip file.zip -d <directory>
Following is a developed version of the above script with error logging and this also deletes the ZIP file after successful unzipping:
#!/bin/sh
for zip in *.zip
do
dirname=`echo $zip | sed 's/\.zip$//'`
if mkdir "$dirname"
then
if cd "$dirname"
then
unzip ../"$zip"
cd ..
# delete the zip file
rm -f $zip
else
echo "Could not unpack $zip - cd failed"
fi
else
echo "Could not unpack $zip - mkdir failed"
fi
done
Save the code in a file with name "unzip.sh" and run the script as:
bash unzip.sh
NEXT STEP: Expand the above script to support nested directories. Try this on your own. The hint is to use the output of find command for simplicity.
With this article at OpenGenus, you must have the complete idea of how to unzip all ZIP files at once.