Search code examples
pythonbashshellfindcut

Unzip a file and copy the contents to different folder based on condition


Folks,

i have a requirement to unzip file and copy the contents of the subdirectories of the unzipped file into different location

For Example:

Filename: temp.zip

unzip temp.zip

we have folder structure like this under temp

 temp/usr/data/keanu/*.pdf's
 temp/usr/data/reaves/*.pdf's

my requirement is to go to the unzipped folders and copy

/keanu *.pdf's to /desti1/
and 
/reaves/*.pdf's to /dest2/

i have tried the below:

unzip.sh <filename>

filename=$1
unzip $filename

//i have stuck here i need to go to unzip folder and find the path and copy those files to different destination

UPDATE on My script unzip the file and recursively copy recommended type of files to destination folder without changing the (by preserving the directory structure)

Filename: unzip.sh

#! /bin/bash
#shopt -s nullglob globstar
filename="$1"
var1=$(sed 's/.\{4\}$//' <<< "$filename")
echo $var1
unzip "$filename"
cd "$(dirname "$filename")"/"$var1"/**/includes
#pwd
#use -udm in cpio to overwrite
find . -name '*.pdf' | cpio -pdm /tmp/test/includes
cd -
cd "$(dirname "$filename")"/"$var1"/**/global
#pwd
find . -name '*.pdf' | cpio -pdm /tmp/test/global

Solution

  • In case the zip is always structured the same:

    #! /bin/bash
    shopt -s nullglob
    filename="$1"
    unzip "$filename"
    cp "$(dirname "$filename")"/temp/usr/data/keanu/*.pdf /desti1/
    cp "$(dirname "$filename")"/temp/usr/data/reaves/*.pdf /desti2/
    

    In case the structure changes and you only know that there are directories keanu/ and reaves/ somewhere:

    #! /bin/bash
    shopt -s nullglob globstar
    filename="$1"
    unzip "$filename"
    cp "$(dirname "$filename")"/**/keanu/*.pdf /desti1/
    cp "$(dirname "$filename")"/**/reaves/*.pdf /desti2/
    

    Both scripts do what you specified but not more than that. The unzipped files are copied over, that is, the original unzipped files will still lay around after the script terminates.