37

When I try

 unzip filename.zip

it works. However, I need to unzip a series of zip files.

Why are:

 find . -name "*.zip" -print0 | xargs -0 unzip

or

 ls *.zip | xargs unzip

not working?

In both cases I get a "caution: filename not matched: " message.

4 Answers 4

51

You can issue the command:

$ unzip '*.zip'

Look here for reference.

2
  • 1
    I didn't say it clearly, but they are not all in the same directory. So, I need the output of find. Aug 21, 2013 at 23:02
  • 4
    Thanks. This might nit match the actual question perfectly but this solved my problem. I was trying unzip * and it returned caution: filename not matched for every file but unzip '*.zip' did the job.
    – Ivan
    Aug 2, 2016 at 3:50
31

Your commands are not working because they stuff all the files onto the same command line. While that works with most programs, unzip will take the first argument as the zip file, and any after the first as files to extract from it. You need to execute the command once for each file:

find . -name "*.zip" -print0 | xargs -0 -n1 unzip

Or

find . -name '*.zip' -exec unzip {} \;
1
  • The second one seems to be better in case there really are a lot of files. (Otherwise it complained the command is too long with xargs)
    – Recct
    Mar 16, 2015 at 11:58
6

In bash you could also do the following:

for i in *.zip; do
    unzip "$i"
done

Also unzip can take the -d switch so you can target the output to different locations.

For example:

uzdir=/path/to/unzips
for i in *.zip; do
    [ -d "$uzdir/$i" ] || mkdir -p "$uzdir/$i"
    unzip "$i" -d "$uzdir/$i"
done
0
2

There is a much easier solution than the looping ones given above, use the directory -d flag:

unzip -o somefiles.zip -d $directoryPath
1
  • -d specifies the dir to extract into, not the dir to look in for files. Jan 4, 2017 at 19:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .