Korn shell script to sync/move files that are not in use

Hello all. This may seem like a dumb/easy question but right now I have a little script I made that uses rsync to sync a directory that has files in it that may or may not be complete files. I want to come up with a better solution for this. What it is is I have a directory lets say /incomplete that has files that could still be downloading or being transferred to that directory. I want to grab files in that /incomplete directory that are complete and move to another directory called /complete. Is there a good way of doing this so that I can only move completed files without corrupting or messing with the files that are not finished transferring to the /incomplete folder? Thanks in advance and I hope that I was clear.

Hi linuxn00b,
Welcome to the forum.

You may want to use man fuser ("fuser") or man lsof ("lsof")

1 Like

@anchal_khare thanks for idea. I will mess around a bit and modify what I have in my script and see if I have anymore questions.

---------- Post updated at 06:17 PM ---------- Previous update was at 06:16 PM ----------

OK I thought I had a good idea of what to do but now I am a little confused. Can you possibly write up a short example of your idea into a little script? Sorry for the lack of knowledge. I can run lsof and see what is being used but then I am not sure what to do to then transfer the files that are not still open. Thanks

This is more than just using rsync as a form of backup. What you really need is a list of files that are opened and then have rsync to exclude to backup those files. However, files could be opened and/or closed at the point when rsync has compiled a list of files to copy and started the backup process.

A better solution is to generate a list of open files and use snapshot to take a static copy of the entire file system. rsync the files from the static file system to the backup destination. At the end, ensure the opened files are closed and re-backup those files that are opened previously.

so I have a few ideas in place here from some stuff I have came across on this forum and online. But I am missing something in each of these scripts. Ideas welcome please. What am I missing here??? :wall:

#!/usr/bin/ksh

FILE_NAME=$i
FILE_OPEN=`lsof | grep $FILE_NAME`
ls -l /usr/local/bin/test | while read a b c d e f g h i; do
	if [ -z $FILE_OPEN ] ;then
		echo "File NOT open"
	else
		echo "File Open"
	fi
done
#!/usr/bin/ksh

file1=`cksum $i | awk '{print $1}'`
sleep 10
file2=`cksum $i | awk '{print $1}'`

ls -l /usr/local/bin/test | while read a b c d e f g h i ; do
	if [  $file1 = $file2 ] ; then
		echo "Both are same" $file1 $file2
	else
		echo "Both are different" $file1 $file2
	fi
done