Joey’s Linux Scams

Unzipping a directory of zip files

Posted by: Joey on: April 12, 2007

Here’s a small bash script that will allow you to unzip a directory full of zip files. Each file will be unzipped into a directory named for itself (e.g. the contents of foo.zip will be extracted to a directory called foo). To run the script, enter the directory where the zip files are stored and create a file with the following contents:

#!/bin/sh
for i in *.zip
do
  mkdir `basename $i .zip`
  unzip $i -d `basename $i .zip`
done

You can name the file whatever you wish. I’ll use unzip_script as an example. We then need to make the file executable. You can do this with the following command:

chmod 755 unzip_script

And now we can run the file with the following command:

./unzip_script

You should now have a directory for each zip file full of it’s contents. You can also modify the script to suit your needs. The following script will delete the zip files once they are extracted:

#!/bin/sh
for i in *.zip
do
  mkdir `basename $i .zip`
  unzip $i -d `basename $i .zip`
  rm $i
done

And here’s another version that moves the zip files to another directory once they are extracted:

#!/bin/sh
mkdir zip_files
for i in *.zip
do
  mkdir `basename $i .zip`
  unzip $i -d `basename $i .zip`
  mv $i zip_files
done

Be sure to make a backup copy of the data to be extracted. These scripts were tested, but I can’t anticipate every possibility. Always take precautions to preserve your data.

Leave a Reply