12

I have a directory full of zip files. Each called something like 'files1.zip'. My instinct is to use a bash for loop to unzip each file.

Trouble is, many of the files will unzip their contents straight into the parent directory, rather then unfolding everything into their own unique directory. So, I get file soup.

I'd like to ensure that 'files1.zip' pours all of it's files into a dir called 'files1', and so on.

As an added complication, some of the filenames have spaces.

How can I do this?

Thanks.

bob
  • 173
  • 2
  • 6

3 Answers3

19
for f in *.zip; do
  dir=${f%.zip}

  unzip -d "./$dir" "./$f"
done
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
0

Simple one liner

$ for file in `ls *.zip`; do unzip $file -d `echo $file | cut -d . -f 1`; done
Konrad
  • 17,740
  • 16
  • 106
  • 167
SJK
  • 91
  • 1
  • 10
-1

you can use -d to unzip to a different directory.

for file in `echo *.zip`; do
    [[ $file =~ ^(.*)\.zip$ ]]
    unzip -d ${BASH_REMATCH[1]} $file
done
Kim Stebel
  • 41,826
  • 12
  • 125
  • 142