Copy/move file which are not currently in use.

Hello experts,
I'm doing a shell script that able to copy or move files.

My situation is i have a drive ( lets say its S drive) where its receive all the files from mainframe

i need to copy all the file from S drive to T drive. but only those files that are finished copied from mainframe and not in use by other applications.

  • i need to check it and
  • i need to create the log files for every copy or move process.

my coding look like this

cd /cygdrive/s/Files
for FILES in ./*.*
do
temp=`basename $FILES`
echo "$temp ">> /cygdrive/s/LogFiles/OriginalFiles.rtf

\#Copy files from S drive to T drive
cp $FILES /cygdrive/t/CopyFile1/$FILES

done
unset temp

But the coding above takes all the files that's currently in use and copying files into log.
Anyone can help me in this???

one way,
reprogram your mainframe code that copied the files to your local to include a dummy file that signifies copying is done. Then you can just check for that done file in your shell script.

Thanks for reply.
But i cant write anything in mainframe code.

Copy files from s drive to t drive, should include only files that already complete copying from mainframe.
Can i use error handling for this problem??
i totally don't know how to do this??

2 other ways, check for changing file size. If they keep changing, then its still in use. you can also try fuser command. check the man page.

In most of the cases, like this, the system are completely decoupled with different assigned owners with different teams, so a consumer team ( receiving files ) is very hard / difficult to change the format/procedure with respect to the producer. Main reason could be,

  1. 1) they don' t want to change simply
  2. 2) producer might be catering to multiple consumers altogether

---------- Post updated at 10:58 AM ---------- Previous update was at 10:56 AM ----------

Check for lsof command as well.
It will show the valid process id attached to a file handle. Probably, if a file is being still copied then process id of the 'copy' process should be intact with the file handler of the file being copied

im using cygwin
there is no lsof in my cygwin.
how to get it???

some more how to get all the files EXCEPT the currently used files using lsof