Home > OS >  Extract a specific *.zip within a *.zip file to a certain directory
Extract a specific *.zip within a *.zip file to a certain directory

Time:09-07

I have multiple SHAPE_*.zip files in a directory, and within each of these .zip files there is another zip file named AREA_IMOVEL.zip. I want to extract the content of this file in a folder with the name of the parent .zip file.

RR/SHAPE_143017.zip
                =======> AREA_IMOVEL.zip
RR/SHAPE_143083.zip
                =======> AREA_IMOVEL.zip
DB/SHAPE_123478.zip
                =======> AREA_IMOVEL.zip
AF/SHAPE_134797.zip
                =======> AREA_IMOVEL.zip

I want to extract them as below:

RR/SHAPE_143017/AREA_IMOVEL.shp & other files
RR/SHAPE_143083/AREA_IMOVEL.shp & other files
DB/SHAPE_123478/AREA_IMOVEL.shp & other files
AF/SHAPE_134797/AREA_IMOVEL.zip & other files

I use the following to get nearly similar results.

find . -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;
find . -name 'AREA_IMOVEL.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;
find . \! -name 'AREA_IMOVEL.*' -delete

CodePudding user response:

ai_zip=AREA_IMOVEL.zip
find . -name '*.zip' | while read top_zip; do
    dir=${top_zip%.zip}
    mkdir -p "$dir"                          || exit 2
    # extract only $ai_zip from $top_zip
    unzip -d "$dir" "$top_zip"     "$ai_zip" || exit 3
    # now unzip $ai_zip itself
    unzip -d "$dir" "$dir/$ai_zip"           || exit 4
    rm "$dir/$ai_zip"
done
  • Related