Unzip All Files In | Subfolders Linux New!
It was a typical Monday morning for John, a system administrator at a large organization. He received an email from his colleague, Alex, asking for help with a task. Alex had a directory with many subfolders, each containing multiple zip files. The task was to unzip all these files and make them easily accessible.
John, being the efficient administrator he was, decided to use the Linux command line to tackle this task. He navigated to the parent directory containing all the subfolders and zip files.
cd /path/to/parent/directory
First, he wanted to see the structure of the directory and understand how many subfolders and zip files he was dealing with.
tree
The output showed a complex directory structure with many subfolders, each containing multiple zip files.
John knew that he could use the unzip command to unzip files, but he needed to find a way to do it recursively for all subfolders. He remembered the -r option, which allows unzip to recurse into subdirectories.
However, instead of running unzip directly, John decided to use find to locate all the zip files first. This approach would give him more control and ensure that he only attempted to unzip files that were actually zip files.
find . -type f -name "*.zip"
This command found all files with the .zip extension in the current directory and its subdirectories. John then piped the output to xargs, which would execute unzip for each file found: unzip all files in subfolders linux
find . -type f -name "*.zip" -print | xargs -I {} unzip {}
But wait, there's a better way! John recalled that unzip has a -d option to specify the output directory. He wanted to unzip all files into their respective subfolders, without mixing files from different subfolders.
After some more research, John discovered the perfect one-liner:
find . -type f -name "*.zip" -exec unzip {} -d {}_unzip \;
This command used find to locate all zip files, and for each file found, it executed unzip with the -d option to unzip the file into a new subfolder named after the original zip file, with _unzip appended to it.
John ran the command, and it worked like magic! All zip files in the subfolders were unzipped into their respective directories. He verified the results and sent a triumphant email to Alex:
Subject: Unzipping success!
Dear Alex,
I hope this email finds you well. I've successfully unzipped all files in the subfolders. The command I used was:
find . -type f -name "*.zip" -exec unzip {} -d {}_unzip \;
This command recursively found all zip files and unzipped them into their respective subfolders. Let me know if you need any further assistance.
Best regards, John
Alex was thrilled to see the unzipped files and thanked John for his help. From that day on, John was known as the "unzip master" among his colleagues.
To unzip all files in subfolders on Linux, the most direct and efficient method is using the command with
. This approach ensures each file is extracted precisely within the subdirectory where it is currently located. Unix & Linux Stack Exchange 1. Basic Recursive Extraction The following command finds every It was a typical Monday morning for John,
file in the current directory and all subfolders and extracts them in their respective locations: find . -name -execdir unzip -o Use code with caution. Copied to clipboard : Starts the search in the current directory. -name "*.zip" : Filters for ZIP files only. : Executes the following command from the subdirectory containing the matched file. unzip -o "{}" to overwrite existing files without prompting. Ask Ubuntu 2. Specialized Scenarios
2. The Limitation of Standard Unzip
The standard unzip command does not natively support recursive directory traversal. Running unzip *.zip in a parent directory will only extract archives located immediately within that directory, ignoring any archives nested in subfolders. Furthermore, standard shell globbing (*) is generally not recursive by default in most POSIX-compliant shells.
3. Method 1: Using find with -exec (Most Common)
The find command is the standard tool for recursively locating files. Combine it with -exec to run unzip on each match.
Table of Contents
- Understanding the Problem
- Prerequisites: Installing
unzip - Method 1: Using
findwith-exec(The Most Common Solution) - Method 2: Using a
forLoop withfind - Method 3: Using
findwithxargs(High Performance) - Method 4: Unzipping into Subfolder Names (Avoiding Clutter)
- Method 5: Using
while readfor Complex Filenames - How to Unzip Only Specific File Types (e.g.,
.zip,.ZIP,.jar) - Best Practice: Prevent Unzipping Already Unzipped Files
- One-Liner Summary for Your Cheatsheet
- Troubleshooting: Permissions, Corrupt Files, Overwrites
- Alternative Tools:
unar,7z, andzipinfo - Conclusion
4. Method 2: Using a for Loop with find
If you prefer readability and more control inside the loop, use a for loop that processes find results.
for zipfile in $(find . -name "*.zip"); do
dir=$(dirname "$zipfile")
unzip -o "$zipfile" -d "$dir"
done
Caveat: This breaks if filenames contain spaces or newlines. While rare for .zip files, it's safer to use:
find . -name "*.zip" -print0 | while IFS= read -r -d '' zipfile; do
unzip -o "$zipfile" -d "$(dirname "$zipfile")"
done
-print0and-d ''handle spaces and special characters correctly.