Unzip a bunch of zips into their own directories
Asked Answered
L

4

16

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?

Latonya answered 17/3, 2010 at 16:10 Comment(1)
This belongs on superuser or serverfaultBeneficent
A
14

Add quotes to handle spaces in filename.

for file in *.zip
do
  unzip -d "${file%.zip}" "$file"
done
Araucania answered 17/3, 2010 at 16:43 Comment(1)
This doesn't work on filenames with spaces. Jefromi's answer does.Gunderson
B
12
for zipfile in *.zip; do
    exdir="${zipfile%.zip}"
    mkdir "$exdir"
    unzip -d "$exdir" "$zipfile"
done
Blowbyblow answered 17/3, 2010 at 16:19 Comment(0)
S
1
for x in $(ls *.zip); do
 dir=${x%%.zip}
 mkdir $dir
 unzip -d $dir $x
done
Storage answered 17/3, 2010 at 16:17 Comment(1)
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!Blowbyblow
G
1

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!

Goner answered 9/11, 2016 at 20:57 Comment(1)
Did not confirm this works but a oneliner is convenient for me.Chios

© 2022 - 2024 — McMap. All rights reserved.