How do I unzip multiple / many files under Linux?

Posted on in Categories , , , , last updated January 19, 2016

I have lots of files in a directory called /disk2/images/. All files are in zip file format, so I am using the following command to extract zip files:
     unzip *.zip
The command result into an error which read as follows:
     caution: filename not matched
How do I unzip multiple or many zip files under a Linux/Unix-like system?

Linux or Unix-like system use the unzip command to list, test, or extract files from a ZIP archive, commonly found on MS-DOS systems.

The problem with multiple zip files on Linux

Assuming that you have four file in a /disk2/images/ directory as follows:

  1. pictures.zip
  2. visit.zip
  3. data.zip
  4. invoices.zip

Let us verify it with the ls command:
$ ls
Sample outputs:

data.zip	invoices.zip	pictures.zip	visit.zip

To unzip all files, enter:
$ unzip *.zip
Sample outputs:

Archive:  data.zip
caution: filename not matched:  invoices.zip
caution: filename not matched:  pictures.zip
caution: filename not matched:  visit.zip

Above error indicate that you used the unzip command wrongly. It means extract invoices.zip, pictures.zip, and visit.zip files from inside the data.zip archive. Your shell expands the command ‘unzip *.zip’ it as follows:
unzip data.zip invoices.zip pictures.zip visit.zip
The solution is pretty simple when you want to unzip the file using the wild card; you have two options as follows.

#1: Unzip Multiple Files Using Single Quote (short version)

The syntax is as follows to unzip multiple files from Linux command line:

unzip '*.zip'

Type the following command as follows:
$ cd /disk2/images/
$ unzip '*.zip'
$ ls -l

Note: *.zip is put in between two single quotes so that shell will not recognize it as a wild card character.

#2: Unzip Multiple Files from Linux Command Line Using Shell For Loop (Long Version)

You can also use for loop as follows:

 for z in *.zip; do unzip $z; done
See also

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

28 comment

  1. # If files are password protected, and we are not sure which password

    for Z_FILE in *.zip; do
    for PASSWD in [ pass123, PASS123, abc123, ABC123 ]; do
    unzip -P $PASSWD $Z_FILE;
    if [ $? = 0 ]; then # successful unzip
    break
    fi
    done
    done

  2. > Appreciate your post.

    No probs, copyleft (c) :)

    Actually I’ve seen yours:

    $ for z in *.zip; do unzip $z; done

    But, it was not enough for me, ’cause we have password protected ones… So, can say that idea comes from you :) I just added a new feature (validation)…

    I think it should/can be improved further to check for other exit status codes…

    Maybe later some day…

  3. I used to use for i in *.zip ; do unzip $i ; done as well but then I found out about the following command and use it all the time now.

    unzip \*.zip

    escape the asterik and you are good to go. :)

  4. Hi

    I tried unzip -P $PASSWD $Z_FILE; command but it is not working and for same zip file it is working in US with same password. I read in one of the web sites that, non USA system needs to install a patch for running above command. If yes Please let me know where can I get this patch else please let me know how to run this command.

    Thanks
    Naresh

  5. If you have spaces in your filenames, you can also use the following:

    for z in *.zip; do unzip “$z”; done

  6. If you like to extract multiple tar or tar.gz use the following command
    for f in *.tar.gz; do tar zxf $f; done

  7. Question: and if I want do delete a file in a batch of different compressed archives? Lets say all copies of “info.txt” or “logo.jpg” in a.zip, b.zip, c.zip(…) z.zip, etc. There is a way to do it?

  8. thanks a lot …..4 help

    if u want to display some msg then…

    for z in *.zip;
    do unzip $z;
    done
    echo "ALL FILE ARE UNZIP SUCCESSFULLY"
  9. Thanks everyone for the answers. If space is an issue, you can do something like (from some dude) to remove the archive files as they are unpackaged:
    for z in *.zip; do unzip $z; rm $z; done

  10. I have 1..10 zip files in one directory …i want to unzip all files at a time..
    can any one help how to do that

  11. I had a pile of zip that each contained a index.html file and the archive structure had no folder.. They obviously had to be extracted in separate folders so as to not overwrite the so precious files. Since it was a temporary “view and delete” kind of thing and with well over a hundred files (not needing to be unzipped in a specific folder, current folder was just fine), i came up with this;

    for z in *.zip; do q=$(echo $z | cut -f 1 -d '.'); unzip $z -d ./$q; done;

    It basically extracts all the zip files to the current directory, into a folder named after the zip filename. Greatly inspired (if not totally ripped from!) NixCraft and http://stackoverflow.com/questions/12152626/how-can-remove-the-extension-of-a-filename-in-a-shell-script

    Couple seconds after i figured “oh well”, i saved that much time so i might as well make something out of it… So instead of just ‘hardcoding’ it into a simple bash alias that doesn’t take parameters, I took a couple more minutes to have it let me at least input a base directory for extraction. Here’s what i came up with (function in .bashrc file);

    unzipALL() {
    dir='.';
    [[ ! -z "${1// }" ]] && dir=$1;
    for z in *.zip; do
    q=$(echo $z | cut -f 1 -d '.');
    unzip $z -d $dir/$q;
    done;
    }

    From the command line just use unzipAll to unzip to the current folder or pass it a folder name: unzipAll /tmp extracts all to /tmp/%filename%.

    It does perform a single basic check on the passed parameter but nothing too fancy (strip spaces before/after then checks against null). One could spend more time and cutomize it further, adding bad directory check and all that…

    I figure i’d share… thanks and hopefuly it can be of use to someone!

    I hope i didn’t break any rules posting a url in here, sorry if so heeh

    1. unzip *.zip does not work in bash.

      [email protected]:~/tmp-zip$ unzip *.zip
      Archive: test1.zip
      caution: filename not matched: test2.zip
      [email protected]:~/tmp-zip$

      unzip \*.zip DOES work, still doesn’t solve the problem i faced of multiple same-name files…

      [email protected]:~/tmp-zip$ unzip \*.zip
      [........]
      inflating: index.html
      inflating: portfolio.html
      inflating: services.html
      Archive: test2.zip
      replace index.html? [y]es, [n]o, [A]ll, [N]one, [r]ename:

      unzip “*.zip” results in the same, btw.

      Hence the quick function. If you have a better way please share
      Also, while testing you assumptions, i found out that unzip tries to look for .ZIP files on it’s own – pretty neat… unzip \* would work (still not help with the issue…)

  12. I just pulled a lot of files (over 400K) all of them were in zip, i’m trying to make huge archive off free documents at http://uploadcoins.com , so for me worked # unzip \*.zip , all that 400K went ok :) , so again, You can use unzip \*.zip , Good luck

  13. I want a script to extract a particular file from the multiple zip folder.

    U:\one\two\CHOS\1094567\1094567_r71.zip\1094567_r41.zip\1094567_21.zip\CHOS_1094567.xml

  14. What should be the command to unzip all the files with a single command if the extension is .gz?

    I used the above unzip ‘*.gz’.But it doesn’t work.

    Archive: zid_ia_rcvbl_acct_eecucl.skp.gz
    End-of-central-directory signature not found. Either this file is not
    a zipfile, or it constitutes one disk of a multi-part archive. In the
    latter case the central directory and zipfile comment will be found on
    the last disk(s) of this archive.

    168 files had no zipfile directory.
    No zipfiles found.

Comments are closed.