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.

ArchiveSingle fileRecursive form
.zipunzip -o a.zip -d dirfind . -name '*.zip' -execdir unzip -o '{}' \;
.tar.gz, .tgztar -xzf a.tar.gz -C dirfind . -name '*.tar.gz' -execdir tar -xzf '{}' \;
.tar.bz2tar -xjf a.tar.bz2find . -name '*.tar.bz2' -execdir tar -xjf '{}' \;
.gz (one file)gunzip a.gzgunzip -r .
.7z7z x a.7z -odirfind . -name '*.7z' -execdir 7z x '{}' \;
.rarunrar x a.rarfind . -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

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the process to hire?
What is coding in computer networks?
What's unique about Shopify?
What Is the Brex Interview Process Like? (Round by Round)
Brex interviews engineers in four to five rounds over about three weeks: recruiter screen, practical coding, a technical deep dive, and a values interview.
What does encapsulation mean?
Leetcode 1249. Minimum Remove to Make Valid Parentheses
Step-by-step Explanations, Python & Java Code Samples, Complexity Analysis, and Interview Preparation Tips
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$99

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.