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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I monitor for a new file is in a directory

Tips and Tricks

How do I monitor for a new file is in a directory

by  gamerland  Posted    (Edited  )
Option 1:
This watches for a new file to show up after "touch time"
and move it to the archive. See grep for all the -exec options.

#!/bin/ksh
#
TIMESTAMP="[color blue]YourPathAndFilenameToTimestampFile[/color]"
FILENAME="[color blue]YourPathAndFilenameHere[/color]"
ARCHIVE="[color blue]YourArchivePathAndNameHere[/color]"
# Set touch date to current date (and time if you need)
touch -t $(date +%Y%m%d0000) $TIMESTAMP
find $FILENAME -newer $TIMESTAMP -exec cp $FILENAME $ARCHIVE {} \;

If you are waiting for a file to arrive so you can do something else. Perhaps you get a file every "X" hours and but it is not scheduled. A file could arrive at 11:00 and 13:00 or 11:00, 12:00 and 12:40.

Option 2:
However if the real issue is waiting for the ftp file to show up, try this: (Korn shell)
#
#!/bin/ksh
#
TIMESTAMP="[color blue]YourPathAndFilenameToTimestampFile[/color]"
WAITINGON="/home/ftpfile"
#
# mark file with start of day time/date
touch -t $(date +%Y%m%d0000) $TIMESTAMP
#
COMPLETE="no"
until [[ $COMPLETE = "yes" ]]; do
if [[ $TIMESTAMP -nt $WAITINGON ]]
then
sleep 600 # 10 minutes [color blue]change according to need[/color]
else
COMPLETE="yes"
fi
done
...remainder of process here.

This way you will start the process and if your file is not there yet, sleep and look again. Once it gets there, off you go. I strongly suggest you put an extra check in that if the file does not show up in a given amount of time it sends an alert/email/notice/etc.

This would be put into cron to run on a schedule near arrival time for the file. It will wait from there.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top