Search code examples
bashunixunzip

Unzipping Zip files having a .done file in unix


Problem statement: I want to unzip files from a folder like /home/a/zip_input where there will be 2 zip files like: /home/a/zip_input a_EOD-20121129.zip b_EOD-20121129.zip

Also there will be 1 corresponding .done file for each each zip(basically once the zip is completely copied a .done file is created) like: a_EOD-20121129.zip.done b_EOD-20121129.zip.done

I am trying to write a script to unzip the files which are having a corresponding .done file, and move the file to a location /home/b/file_input. After the file is moved compeltely I want to create a file with .done ext. For eample: each zip is having one xml file like a_EOD-20121129.xml/b_EOD-20121129.xml

so the output folder will have something like:

a_EOD-20121129.xml a_EOD-20121129.xml.done b_EOD-20121129.xml b_EOD-20121129.xml.done

What I have tried Till now:

for file in $(find . -name "*.zip" -type f)
do
    unzip $file -d /home/b/file_input
    rm $file    
done

Which unzips a.zip/b.zip and creates the xml files.

Issue I am facing -

  1. How to make the condition in script that only picks files having a corresponding .done file?

  2. How to create a corresponding .done file after the file is unzipped and moved.(have to take the file name and create a file with same name appending .done)?


Solution

  • You can try the following script(Not tested):

    for file in $(find . -name "*.zip" -type f)
        do
          if [ -f $file".done" ];    then         #test if there is a done file for that zip
              unzip $file -d /home/b/file_input
              for filezip in `unzip -l $file | awk '/.xml$/{print $NF}'`; do  #creates the .done files for each file inside the zip
                 touch /home/b/file_input/$filezip".done"
              done
              rm $file    
          fi
        done