Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uwm.edu!psuvax1!rutgers!att!chinet!les From: les@chinet.chi.il.us (Leslie Mikesell) Newsgroups: comp.unix.questions,hp.unix Subject: Re: how do you watch for an incoming file Keywords: file directory unix Message-ID: <1989Nov29.181559.2421@chinet.chi.il.us> Date: 29 Nov 89 18:15:59 GMT References: <5506@hplabsb.HP.COM> Reply-To: les@chinet.chi.il.us (Leslie Mikesell) Organization: Chinet - Chicago Public Access UNIX Lines: 38 In article <5506@hplabsb.HP.COM> quan@hplabsb.HP.COM (Suu Quan) writes: > > How do I write a C-program or shell script that will watch for >the presence of a new file in a certain directory. > >Scenario : I expect a file to be sent to me -name possibly unknown- to a >directory (callit /application/inputfile/). Since I do not know when it >will arrive (ftp, rcp ... does not matter), I want to be notified of its >presence as soon as possible but not burn any cpu in the mean time. The cheapest operation would probably be to stat(2) the directory at some interval, then use opendir(), readdir(), etc. to find the filename when the st_mtime field changes. For a shell approach you will have to read the directory but the (generally) built-in echo command will do it. # cd dir while : do FILES=`echo *` if [ $FILES != "*" ] then # put your notify command here... exit fi sleep 30 done > But I would prefer a C-program or a script using a blocking command > Any help is appreciated That would require some help from the delivery program - for example writing to a FIFO when the delivery is complete. In any case you might need to guard against processing a partial file (i.e. the name has appeared but the writer has not completed its output). Les Mikesell les@chinet.chi.il.us