Unzip All Files In Subfolders Linux
Sometimes zip files contain many loose files. To keep things organized, extract each zip into a new subfolder named after the zip file (without the .zip extension).
find . -name "*.zip" -exec sh -c '
target="$0%.zip"
mkdir -p "$target"
unzip -o "$0" -d "$target"
' {} \;
Example result:
data1/
├── images.zip
└── images/ # extracted contents go here
├── photo1.jpg
└── photo2.jpg
This method prevents naming conflicts if multiple zip files contain files with identical names. unzip all files in subfolders linux
To avoid conflicts, create a directory for each archive: Sometimes zip files contain many loose files
find /path/to/root -type f -iname '*.zip' -print0 | while IFS= read -r -d '' zip; do
dir="$(dirname "$zip")"
base="$(basename "$zip" .zip)"
dest="$dir/$base"
mkdir -p "$dest"
unzip -q "$zip" -d "$dest"
done
When keeping original tree intact and extracting into a separate root: Example result: data1/ ├── images
src="/path/to/root"
dst="/path/to/extracted"
find "$src" -type f -iname '*.zip' -print0 | while IFS= read -r -d '' zip; do
rel="$zip#$src/"
reldir="$(dirname "$rel")"
mkdir -p "$dst/$reldir"
unzip -q "$zip" -d "$dst/$reldir"
done
In Linux system administration and data processing workflows, it is common to encounter directories containing multiple ZIP archives distributed across various subfolders. Manually navigating into each subdirectory and running unzip is time-consuming and error-prone. This paper provides a systematic overview of methods to recursively locate and extract all ZIP files within a directory tree using standard Linux command-line tools.
| Problem | Solution |
|---------|----------|
| ZIP files with spaces in names | Use -print0 and xargs -0 or properly quote "{}" |
| Extracting into wrong directory | Always test with echo before running: find ... -exec echo unzip {} \; |
| Permission denied | Run with sudo or change ownership of target directories |
| Too many arguments error | Use find + xargs instead of shell globs (*.zip) |