Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to compare file size before execution

Status
Not open for further replies.

pcutler

Technical User
Jan 18, 2002
59
CA
Hi,

I’m quite new to Linux and I’m looking for some help with a script I’m working on.

I have setup an FTP server to receive fairly large files (5-10 meg). The script I’m working on takes the files that have been uploaded onto the FTP site and then FTP’s them to another server.

The script is run by the crontab every 2 minutes. My problem is that the script is trying to move files that are still being uploaded onto the FTP site by our users.

How can I set my script so that it only moves files whose size hasn’t changed for at least 10 seconds?

Any help you can offer is appreciated.

PC
 
Not sure exactly, but I would think the logic for what you are asking would be something like this:

get filesize1

sleep 10

get filesize2

does filesize1 == filesize2?

if yes move it.

else exit.
 
Thanks for your reply.

Would this work in a situation where there are several files in the directory, some with static file sizes and some still growing?

Thanks again.

PC
 
Hi,
this might work to identify files modified over 1 minute ago

cd ftpfolder
find . -cmin +1 > filereadylist

Do you really need to run a cron job every 2 minutes? It seems like you are creating a lot of system overhead.
 
The easiest way to do this is to write a small state
daemon. Of course this would take some doing.
perl or tcl would be easier than the shell since you
would end up juggling at least 2 shell arrays.
As a challenge I'm trying to hack something out in C
right now. If it works i'll post it.
 
Not sure if this applies to Linux but on other Unix variants &quot;fuser <filename>&quot; will tell you if any processes have the file open. Your script could call &quot;fuser <filename>&quot; and only transfer files that are not currently open.
 
#!/usr/bin/tclsh
#trivial directory watcher
set parent [lindex $argv 0]
set waittime [expr {[string length [lindex $argv 1]] > 0 ? [expr [lindex $argv 1] * 1000] : 10000}]
set cnt 0
array set a1 {}
array set a2 {}

proc clrScrn {x} {
set p 0
while {$p < $x} {
puts &quot;\n&quot;
incr p
}
return
}

proc gatherFileInfo {parent} {
global a1 a2 cnt
foreach fcan [glob -nocomplain $parent] {
if {[file isfile $fcan]} {
set a1([file tail $fcan]) [file size $fcan]

}
}

compFiles
set d [array size a2]
puts $d
if {$cnt == 0} {
parray a1
copyToStore
puts &quot;At count $cnt&quot;
incr cnt
return [array size a1]
} elseif {$d > 0} {
puts &quot;Changed files: &quot;
parray a2
copyToStore
puts &quot;At count $cnt&quot;
incr cnt
return [array size a1]
} elseif {$d < 1} {
puts &quot;No change&quot;
puts &quot;At count $cnt&quot;
parray a1
incr cnt
copyToStore
return [array size a1]
}
return 1
}

proc copyToStore {} {
global a1 a2

foreach elem [array name a1] {
set a2($elem) $a1($elem)
}
return
}

proc eloop {} {
global waittime parent
clrScrn 25
gatherFileInfo $parent
after $waittime {eloop}
}


proc compFiles {} {
global a1 a2

foreach elem [array name a1] {
foreach poss [array name a2] {
if {[string compare $elem $poss] == 0} {
if {$a1($elem) == $a2($elem)} {
puts &quot;No change for $elem&quot;
unset a1($elem) ; unset a2($elem)
}
}
}
}
return [array size a1]
}

eloop
vwait forever

Run it like:
tclsh scriptname globpatterndir timeinseconds
ex: tclsh mywatcher.tcl /home/me/* 10
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top