Wednesday, October 24, 2012

Create new file depending on the last modified time



The following script would automatically create .trg files if a file doesn’t exist. It also checks for the file last modified time. Here I have placed it as 5 minutes. Only if the file has not been modified for last 5 minutes it goes and creates the .trg file. The timing can be extended as needed. This should prevent the script from creating .trg files for the files that are getting processed. 


#!/bin/bash

cd /hulanas/smartdoc/inbound

for file in *.xml
do
# check if .trg file already exists, if yes ignore
  if [ -a $file.trg ]
    then
       echo "$file.trg already exists"
# check if the file has not been modified for more than 5minutes (time can be increased depending on the requirement)
# if yes, go ahead and create the .trg file.
# This is to prevent .trg files from being created for the files that 
# might be getting processed.
  elif [ `find $file -type f -mmin +5` ]
     then
# create the .trg file
        touch $file.trg
  fi
done