292

I have a directory of ZIP files (created on a Windows machine). I can manually unzip them using unzip filename, but how can I unzip all the ZIP files in the current folder via the shell?

Using Ubuntu Linux Server.

1
  • for windows in powershell: Get-ChildItem 'path to folder' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force
    – Jon
    Nov 10, 2020 at 13:18

17 Answers 17

542

This works in bash, according to this link:

unzip \*.zip

7
  • 6
    I needed the backslash with Zsh. I don't know of a reason you'd need it if you're using Bash. Oct 27, 2015 at 6:23
  • 2
    What will happen with duplicate files? I guess it will just override?
    – kerner1000
    May 23, 2018 at 18:29
  • 4
    When doing this I get filename not matched for all the zip files in the directory Feb 10, 2019 at 10:42
  • 2
    The version with quotes worked for me on WSL. The filenames also had spaces in them, FWIW. Mar 31, 2019 at 13:47
  • 7
    When you get the Filename not matched, just wrap the pattern in single quotes: unzip '*.zip'
    – kano
    May 10, 2021 at 19:22
161

Just put in some quotes to escape the wildcard:

unzip "*.zip"
4
  • 2
    +1 This one worked for me. I had to unzip filenames with a particular format while restricting the rest. I just kept the matching format within double quotes and it worked like charm. Output tells me the number of archives successfully processed. Feb 10, 2015 at 5:28
  • 1
    Worked beautifully on Ubuntu for Windows subsystem, 11/18/2018. Top answer didn't work. Nov 18, 2018 at 15:41
  • 2
    how can we extract them in directories with their respective names?
    – Coddy
    Dec 16, 2020 at 0:32
  • 1
    It is important to use quotes or single quotes. Sample: unzip '*.zip' -d ./myfolder/.
    – deadfish
    Apr 28 at 7:12
113

The shell script below extracts all zip files in the current directory into new dirs with the filename of the zip file, i.e.:

The following files:

myfile1.zip
myfile2.zip 

Will be extracted to:

./myfile1/files...
./myfile2/files...

Shell script:

#!/bin/sh
for zip in *.zip
do
  dirname=`echo $zip | sed 's/\.zip$//'`
  if mkdir "$dirname"
  then
    if cd "$dirname"
    then
      unzip ../"$zip"
      cd ..
      # rm -f $zip # Uncomment to delete the original zip file
    else
      echo "Could not unpack $zip - cd failed"
    fi
  else
    echo "Could not unpack $zip - mkdir failed"
  fi
done

Source Gist


Usage:

cd /dir/with/zips
wget -O - https://www.toptal.com/developers/hastebin/suvefuxuxo.bash | bash
6
  • 10
    This is the thing to conquer all things. Can't believe it's not voted higher Nov 11, 2015 at 22:04
  • This doesn't deal with spaces in filenames. Nov 17, 2016 at 18:54
  • 1
    Just add quotes " to the filename Dec 25, 2016 at 13:38
  • 1
    the ` saved my day! thanks! I am doing some loop, unzip, perform an action, copy, grep something, remove. The thing missing was how to go from file.gz to file as a variable in the bash script
    – thahgr
    Nov 22, 2017 at 14:19
  • 1
    This should be the ultimate answer to all unzipping anywhere and anytime, why is this not the accepted answer? :) Oct 8, 2021 at 12:18
49

unzip *.zip, or if they are in subfolders, then something like

find . -name "*.zip" -exec unzip {} \;
5
  • unzip does wildcard processing so a file called "*.zip" won't do what you expect.
    – geocar
    Mar 3, 2010 at 20:52
  • 4
    Actually this will do exactly what is expected, the result of the find operation is being passed to unzip Aug 13, 2015 at 2:20
  • 4
    This will extract all the zip files in current directory, what if I want the zip files (present in subfolders) to be extracted in the respective subfolders ? Feb 16, 2018 at 8:44
  • 2
    @RishabhAgrahari I addressed your comment in a new answer.
    – j-i-l
    Apr 15, 2018 at 19:26
  • for gzip'ed files, use gunzip -rfk . for recursive unzipping inside respective folders
    – Devaroop
    Mar 13, 2019 at 9:49
47

Unzip all .zip files and store the content in a new folder with the same name and in the same folder as the .zip file:

find . -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;

This is an extension of @phatmanace's answer and addresses @RishabhAgrahari's comment:

This will extract all the zip files in current directory, what if I want the zip files (present in subfolders) to be extracted in the respective subfolders ?

4
  • 2
    for gzip'ed files, use gunzip -rfk . for recursive unzipping inside respective folders
    – Devaroop
    Mar 13, 2019 at 9:49
  • 2
    Getting find: illegal option -- n using UnZip 6.00 - anybody else? Mar 14, 2019 at 8:33
  • 2
    find . -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \; You forgot the directory listing.
    – SaundersB
    Feb 8, 2020 at 4:32
  • 1
    this worked for me, above script / solutions dont. Aug 21, 2022 at 14:49
23
for i in *.zip; do
  newdir="${i:0:-4}" && mkdir "$newdir"
  unzip "$i" -d  "$newdir"
done

This will unzip all the zip archives into new folders named with the filenames of the zip archives.

a.zip b.zip c.zip will be unzipped into a b c folders respectively.

1
  • This one worked for my use case, needs more up votes. The other approaches do not place the extracted files in a folder of the same name, as expected, but there are some cases where using this approach to separate the folders will be needed.
    – greg
    Feb 2, 2022 at 20:13
8

In any POSIX shell, this will unzip into a different directory for each zip file:

for file in *.zip
do
    directory="${file%.zip}"
    unzip "$file" -d "$directory"
done
1
  • 1
    Or as a one-liner: for file in *.zip; do unzip "$file" -d "${file%.zip}"; done
    – peterflynn
    Sep 19, 2018 at 21:45
7

aunpack -e *.zip, with atool installed. Has the advantage that it deals intelligently with errors, and always unpacks into subdirectories unless the zip contains only one file . Thus, there is no danger of polluting the current directory with masses of files, as there is with unzip on a zip with no directory structure.

1
  • aunpack -e -D *.zip if you want each zip to get its own output dir regardless of the number of files in it (similar to default behavior of ExtractAll in Windows)
    – teichert
    Dec 22, 2021 at 5:38
5

for file in 'ls *.zip'; do unzip "${file}" -d "${file:0:-4}"; done

2
  • 1
    Great to me. Unzip in their subfolders respectively
    – insign
    Aug 24, 2018 at 19:44
  • It's safer to just do for file in *.zip; do ... right?
    – peterflynn
    Sep 19, 2018 at 21:42
5

If by 'current directory' you mean the directory in which the zip file is, then I would use this command:

find . -name '*.zip' -execdir unzip {} \; 

excerpt from find's man page

-execdir command ;
-execdir command {} +

Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. As with the -exec option, the '+' form of -execdir will build a command line to process more than one matched file, but any given invocation of command will only list files that exist in the same subdirectory. If you use this option, you must ensure that your $PATH environment variable does not reference the current directory; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir.

3

Use this:

for file in `ls *.Zip`; do
unzip ${file} -d ${unzip_dir_loc}
done
2

Here is a one liner without using ls that creates folders with the zip names for the files. It works for any zips in the current directory.

for z in *.zip; do unzip "$z" -d "${z%".zip"}"; done

You can add it to your .bashrc

alias unzip_all='for z in *.zip; do unzip "$z" -d "${z%".zip"}"; done'

Inspiration taken from:

Method #2: Unzipping Multiple Files from Linux Command Line Using Shell For Loop (Long Version) in https://www.cyberciti.biz/faq/linux-unix-shell-unzipping-many-zip-files/

1

If the files are gzip'd. Then just use:

gunzip -rfk .

from the root directory to recursively extract files in respective directories by keeping the original ones (or remove -k to delete them)

0

This is a variant of Pedro Lobito answer using How to loop through a directory recursively to delete files with certain extensions teachings:

shopt -s globstar
root_directory="."

for zip_file_name in **/*.{zip,sublime\-package}; do
    directory_name=`echo $zip_file_name | sed 's/\.\(zip\|sublime\-package\)$//'`
    printf "Unpacking zip file \`$root_directory/$zip_file_name\`...\n"

    if [ -f "$root_directory/$zip_file_name" ]; then
        mkdir -p "$root_directory/$directory_name"
        unzip -o -q "$root_directory/$zip_file_name" -d "$directory_name"

        # Some files have the executable flag and were not being deleted because of it.
        # chmod -x "$root_directory/$zip_file_name"
        # rm -f "$root_directory/$zip_file_name"
    fi
done
-1
for i in `ls *.zip`; do unzip $i; done
1
  • 17
    Dangerous use of ls. What if you have a file called "-j -o -d .. jackass.zip"
    – geocar
    Mar 3, 2010 at 20:50
-1

Use

sudo apt-get install unzip 

unzip file.zip -d path_to_destination_folder

to unzip a folder in linux

2
  • This will only unzip single file. Question is to unzip all folders Apr 21, 2015 at 8:43
  • To be fair, the title "Unzip All Files In A Directory" could be ambiguous and also mean to "Unzip a single archive into a separate directory". Mar 2, 2018 at 3:37
-3

To unzip all files in a directory just type this cmd in terminal:

unzip '*.zip'
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.