How to Recursively Unzip Archives in a Directory and Its Subdirectories on Linux
To unzip every .zip file in a directory tree, each into the folder where it sits, run one find command.
find . -type f -name '*.zip' -execdir unzip -o '{}' \;
find . walks the current directory and every subdirectory. -type f -name '*.zip' keeps regular files ending in .zip. -execdir runs the command from the directory that contains each file, so unzip extracts next to the archive. -o overwrites existing files without asking, which matters when the command runs unattended; drop it, or use -n to never overwrite.
Spaces and other unusual characters in file names are safe here. find passes each path to unzip as one argument, with no shell splitting.
Extract each archive into its own folder
The command above puts the contents of a.zip into the same folder as a.zip. To give each archive its own folder, pass the archive name without the extension to -d.
find . -type f -name '*.zip' -exec sh -c 'unzip -o "$1" -d "${1%.zip}"' _ {} \;
sh -c is needed because ${1%.zip}, which strips the suffix, is shell syntax. find does not run a shell on its own. The _ fills $0 so that the path arrives as $1. With this form, docs/report.zip is extracted into docs/report/.
To send everything to one target directory instead, use a fixed -d.
find . -type f -name '*.zip' -exec unzip -o {} -d /tmp/extracted \;
Files with the same name inside different archives will overwrite each other in that case.
The loop version
A while loop is easier to extend with logging or a check on each file. Use -print0 and read -d '' so that names with spaces or newlines survive.
find . -type f -name '*.zip' -print0 | while IFS= read -r -d '' f; do echo "extracting $f" unzip -oq "$f" -d "$(dirname "$f")" || echo "failed: $f" >&2 done
-q suppresses the per-file listing that unzip prints. IFS= read -r keeps leading spaces and backslashes in the name.
Nested zips: archives inside archives
One pass extracts only the archives that exist when find starts. If an archive contains further .zip files, run the pass again until none are left. Each archive must be removed or renamed after extraction, or the loop never ends.
while [ -n "$(find . -type f -name '*.zip' -print -quit)" ]; do find . -type f -name '*.zip' -print0 | while IFS= read -r -d '' f; do if unzip -oq "$f" -d "${f%.zip}"; then rm "$f" else mv "$f" "$f.failed" fi done done
-print -quit makes the outer test cheap, because find stops at the first match. A corrupt archive is renamed to .failed, which takes it out of the *.zip pattern, so the loop still terminates.
Other archive types
unzip handles only the zip format. The pattern is the same for the others; only the extract command changes.
| Archive | Single file | Recursive form |
|---|---|---|
.zip | unzip -o a.zip -d dir | find . -name '*.zip' -execdir unzip -o '{}' \; |
.tar.gz, .tgz | tar -xzf a.tar.gz -C dir | find . -name '*.tar.gz' -execdir tar -xzf '{}' \; |
.tar.bz2 | tar -xjf a.tar.bz2 | find . -name '*.tar.bz2' -execdir tar -xjf '{}' \; |
.gz (one file) | gunzip a.gz | gunzip -r . |
.7z | 7z x a.7z -odir | find . -name '*.7z' -execdir 7z x '{}' \; |
.rar | unrar x a.rar | find . -name '*.rar' -execdir unrar x '{}' \; |
gunzip -r recurses on its own and replaces each .gz file with its uncompressed version. GNU tar detects the compression when extracting, so tar -xf works for .gz, .bz2 and .xz without the z, j or J flag. 7z x takes its output folder in -o with no space before the name.
"unzip: command not found"
Minimal server and container images often ship without unzip. Install it with the distribution's package manager.
sudo apt install unzip # Debian, Ubuntu sudo dnf install unzip # Fedora, RHEL 8 and later, Rocky, Alma sudo yum install unzip # older RHEL and CentOS apk add unzip # Alpine, as root
When you cannot install packages, the Python standard library can extract a zip.
python3 -m zipfile -e archive.zip target_dir/
macOS includes unzip, and so do most desktop Linux distributions.
Zipping a tree the other way
To create an archive of a folder and everything under it, the flag is -r.
zip -r project.zip project/
Without -r, zip stores the folder entry itself and none of its contents.
Key Takeaways
findplus-execdir unzipis the whole answer for one level of archives.- Use
-d "${f%.zip}"to give each archive its own folder, and a fixed-dto collect everything in one place. - Nested archives need a loop that repeats until no
.zipremains, and each extracted archive must be removed or renamed. - Quote every path, or use
-print0withread -d '', so that spaces in names do not break the command. - Shell fluency saves time in daily work, while coding rounds test algorithm patterns. Grokking the Coding Interview teaches those patterns. Grokking Data Structures for Coding Interviews covers the structures under them. A mock interview shows how you explain a solution out loud.

GET YOUR FREE
Coding Questions Catalog

$99

$197

$72