14

I have a bunch of zip files I want to unzip in Linux into their own directory. For example:

a1.zip a2.zip b1.zip b2.zip

would be unzipped into:

a1 a2 b1 b2

respectively. Is there any easy way to do this?

Alexander
  • 141
  • 1
  • 1
  • 3

4 Answers4

14

Add quotes to handle spaces in filename.

for file in *.zip
do
  unzip -d "${file%.zip}" "$file"
done
Dave
  • 598
  • 6
  • 13
ghostdog74
  • 307,646
  • 55
  • 250
  • 337
12
for zipfile in *.zip; do
    exdir="${zipfile%.zip}"
    mkdir "$exdir"
    unzip -d "$exdir" "$zipfile"
done
Cascabel
  • 451,903
  • 67
  • 363
  • 314
1
for x in $(ls *.zip); do
 dir=${x%%.zip}
 mkdir $dir
 unzip -d $dir $x
done
Steve B.
  • 52,726
  • 11
  • 92
  • 128
  • You just barely beat me, but enough differences I posted anyway - you don't need to use ls (the globbing will expand just fine on its own), you don't need `%%` (it deletes the longest match, and there's only one possible match, picky, I know), and it's always good to quote your filenames! – Cascabel Mar 17 '10 at 16:21
0

Sorry for contributing to an old post, this works in cmd line for me and it was a life saver when I learnt about it

for file in $(ls *.zip); do unzip $file -d $(echo $file | cut -d . -f 1); done

Hey presto!

jhscheer
  • 342
  • 1
  • 7
  • 17
SJK
  • 83
  • 1
  • 10