Hello gurus, this should be simple but I cant seem to figure this out. Please assist.
I have several thousand zip files in a directory
A.ZIP
B.ZIP
.
.
..
Z.ZIP
All these zipped files have a single subdirectory at level1 and then other files and folders under the subdirectory.
Example
A.ZIP
------Subfolder12
------------FilesandSubSubFolders
If I unzip A.ZIP, the unzipped folder is called Subfolder12, which I want to be named as A
If I use
unzip A.zip
The result is
Subfolder12
--------FilesandSubfolders
If I do
unzip A.zip -d A
the result is
A
-----Subfolder12
-------------FilesandSubfolders
What I want is
A
------FilesandSubfolders
i`m trying to do this in a loop.
for file in *.zip
do
unzip $file -d ${file%.zip}
done
Aia
January 25, 2016, 2:50pm
2
Perhaps
for f in *.zip; do
if [[ -e "$f" ]]; then
unzip "$f" && mv -v "Subfolder12" "${f%.zip}"
fi
done
Untested.
1 Like
The subfolder is not necessarily called subfolder12, its any random name at level1. So every zip will have a different name for the subfolder at level 1.
Aia
January 25, 2016, 3:30pm
4
In that case we have to find out what's that first directory's name. I am using unzip to tell me.
for f in *.zip; do
if [[ -e "$f" ]]; then
unzip "$f"
firstdir=$(unzip -qql "$f" | head -1)
mv -v "${firstdir##* }" "${f%.zip}"
fi
done
If you have a tar utility that supports --strip-componets:
for f in *.zip; do
if [[ -e "$f" ]]; then
mkdir "${f%.zip}" && tar xvzf "$f" --strip-components=1 -C "${f%.zip}"
fi
done
Again, all these are edited on the fly. Untested.
1 Like