If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Monitor a directory for a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-03, 19:42
heprox heprox is offline
Registered User
 
Join Date: Oct 2003
Posts: 54
Question Monitor a directory for a file

I have a file system called /datafiles. Within this file system I allow remote users to upload files to me with the structure of "upload.0002", "upload.0003", "upload.0004", etc. I want to monitor this directory with a Korn script running at certain times via crontab, that will identify when one of these files exist then run a series of commands on the file that is found. I need to:

1) Be able to identify the file when it is present and has been completely uploaded

2) Discover the files extension (i.e. 0002, 0003, 0004, etc.)

3) Run a series of commands of the file using its extension as an argument.
Reply With Quote
  #2 (permalink)  
Old 12-02-03, 04:27
ndu35 ndu35 is offline
Registered User
 
Join Date: May 2003
Location: France
Posts: 112
Re: Monitor a directory for a file

hi,

find your_dir -name 'upload*' |wc -l gives you number of upload file in your directory

ls -l your_dir | awk '{ FS="." ;print $2 }' gives you file extension.

hope this help you

Quote:
Originally posted by heprox
I have a file system called /datafiles. Within this file system I allow remote users to upload files to me with the structure of "upload.0002", "upload.0003", "upload.0004", etc. I want to monitor this directory with a Korn script running at certain times via crontab, that will identify when one of these files exist then run a series of commands on the file that is found. I need to:

1) Be able to identify the file when it is present and has been completely uploaded

2) Discover the files extension (i.e. 0002, 0003, 0004, etc.)

3) Run a series of commands of the file using its extension as an argument.
Reply With Quote
  #3 (permalink)  
Old 12-02-03, 05:00
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
The following (untested) code tests if a file has updated in the last 60 seconds. If it hasn't, assume it is completely uploaded and process it.

HTH

processFile ()
{
local fileName=$1
local fileExtension=$2
# doStuffWithFile
}

# Main Processing

for fileName in uploadDirectory/upload.[0-9][0-9][0-9][0-9]
do
fileExtension=${fileName#*.}
fileTest=/tmp/$fileName.$(date +%d%m%y_%H%M%S)
cp -f $fileName $fileTest
sleep 60
test "$fileName" != "$(find $fileName -newer $fileTest)" > /dev/null && processFile fileName fileExtension
done
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On