I have the below code which checks if the file in the directory is in copy/load status from another application in a directory.


for file in PATH/*

    lsofresult=`lsof | grep $file | wc -l`
    while [ $lsofresult != 0 ]; do 
        echo "still copying file $file..."
        sleep 1
        lsofresult=`lsof | grep $file | wc -l`

The return code of the loop will be used for the further logic. But if files keep on coming into the directory this loop is keep on executing. But I want to work on the files which are completed copying to the directory from another application.

So, the ideal scenario is if 2 files are copying to the directory in which 1 file has completed copying, I want to work on the completed file as soon as the copying is completed and the another file which will be in copying state needs to work upon completion. Is there any option to differentiate the copied and copying files in a directory? If so can anyone please help with this?

  • So I added a script to my comment/answer check it out @unixlearner
    – Ziazis
    Commented Aug 25, 2017 at 9:23

1 Answer 1


You have a logical error in your thinking, if I understood you correct that is. You are trying to check if one file is finished copying while another file is still copying. So far so good.

BUT I guess that you are using something similar to your for PATH/* - maybe cp path/* destination/*? - , which would make the command copy one file after another, but at the same time you want to check in the background for all files simultaniously if there is a cp command running and if not you want to do something else with them. How would you make sure that your cp had copied your file already and not just hasn't been there yet? Won't work with your psof, at least not if you want to check them at the same time.

What you actually have to do is to check if the file already exists at the destination and has the same hash sum as the original file, only then can you be sure that your file was copied successfully.

This should give you a better idea what to aim for and why your script won't work the way you want it to at the moment.

Unfortunatly this was a little long for a comment.

I actually sat down and created a somewhat script which you probably can use for what you described. Not sure if it is optimized but at least the checks are run at the same time as the copy is going on and it proceeds to do something after the copy is finished:

#! /bin/bash
FILES=`find $SOURCEPATH -type f`

function copy {



function nextfunction {
#Could also do something else with the file already
echo "Done copying" $1

function check {
#Output so we see that the checks run simultaniously
echo "original path variable:" $1
echo "filename:" ${1##*/}

hashOfInputFile=`md5sum $1 | cut -d" " -f1`

#make sure the file actually exists before trying to create a hashsum
while [ ! -f $DESTINATION/${1##*/} ]; do 
sleep 5

#create a first hashsum to check if the file is completly copied
hashOfCopiedFile=`md5sum $DESTINATION/${1##*/} | cut -d" " -f1`

#check if the hashsums are the same if not wait a little and create a new one
while [ "$hashOfInputFile" != "$hashOfCopiedFile" ]; do
sleep 5
hashOfCopiedFile=`md5sum $DESTINATION/${1##*/} | cut -d" " -f1`

#proceed to do something else with the source file 
nextfunction $1

#put the copy in the background so we can proceed with the script
copy &

for file in $FILES 
#checks are also put in the background so the script continues
check $file &
  • Actually this source and destination concept is not applicable in my script where where source will be in a entirely new different and destination will be in a different server. So i cant get the hashsum of the file in the source path where mainframe is used as a middleware for the copying of files from source server path to destination sever path. Thanks for the efforts anyway. Commented Aug 28, 2017 at 8:02
  • Mount the server share and still use it that way? If you use scp/rsync to copy, you might aswell use sshfs.
    – Ziazis
    Commented Aug 28, 2017 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .