Check existence of a number of files and call other scripts

Hi,
I am new to unix scripting and am jus getting to learn about it..
I need to know on how to check for the existence of a number of files in a path..i.e the files are ftp'ed from several other servers, should check if all the files have arrived, if not wait till they arrive..can i use a flag here to have a count on the number of file arrived? if yes, please let me know..once all the files have arrived..should call other scripts for execution which are to be sequentially executed..please help me on this!

Hi yohasini,

  • I'm not sure if you have the total number of files to be transfered, if yes, try with:
files=$(find . -type f | wc -l)

if [ $files -eq 221 ] # total number of files for this example = 221
then
echo "Total # of files = $files, ftp tranfer is completed"
./yourscript1.sh
./yourscript2.sh
.
.
else
echo "Wait, only $files files transfered, $[221-$files] files pending to be received"
fi

You have to verify (send this "if" script) manually, if you need to have a continuous verification, a cron job
would be necessary to send it every certain periods.

Hope it helps,

Regards

Hi kamal,
thanks for the info..but what if i 'd want to check if particularly named files say "newfile.servername_date.dat" have been ftp'ed in the particular folder. and i am aware of the number of files, they are 16.

Hi yohasini,

Then you can add "-name..." to specify the type of files as follow:

files=$(find . -type f -name '*.*_*.dat' | wc -l)

if [ $files -eq 16 ]
then
echo "Total # of files = $files, ftp tranfer is completed"
./yourscript1.sh
./yourscript2.sh
.
.
else
echo "Wait, only $files files transfered, $[16-$files] files pending to be received"
fi

If the format is fixed as you say "newfile.servername_date.dat" use the regex *.*_*.dat as above, if not you can use only *.dat.

Regards.

Yes kamal. The name of the flat file would be something like OBJ_FULL.servername.date(i.e every sunday's date(2011-3-20)).dat). How do i pull off the latest files of this kind from this folder? how do i incorporate it with find?

Hi yohasini,

To find the latest files of this kind, you need to specify which latest files, I mean, files older than 1 day, files older than 1 year, etc. See next example info:

# 1-) Looking for *_*.* files older than 365 days and printing them within folder and subfolder
 find . -name '*_*.*.dat' -mtime +365

# 2-) Printing *_*.* files files modified less than 365 days ago within folder and subfolders
 find . -name '*_*.*.dat' -mtime -365

# 3-) Printing *_*.* files files modified 365 days ago within folder and subfolders
 find . -name '*_*.*.dat' -mtime 365

About -mtime: (-mtime n)
(+) means greater than, (-) means less than, and without any symbol means exactly equal to.

Another example:

#Looking files of kind "*_*.*.dat" modified less than 60 days ago
find . -type f -name '*_*.*.dat' -mtime -60 
./OBJ_FULL.servername1.2011-02-13.dat

#Looking files of kind "*_*.*.dat" modified less than 70 days ago
 find . -type f -name '*_*.*.dat' -mtime -70
./OBJ_FULL.servername.2011-01-22.dat
./OBJ_FULL.servername1.2011-02-13.dat

Only replace the find command in the script with the find command you need with -mtime option you want.

files=$(find . -type f -name '*_*.*.dat' -mtime -70 | wc -l)

Hope it helps,

Regards.