how to check files

Hi,

I have a dir in which I need to check for 3 files. and naming for three files are as below.

fileone_yyyy_mm_dd.dat
filetwo_yyyy_mm_dd.dat
filethree_yyyy_mm_dd.dat

and YYYY_mm_dd will change everyday as the date changes. I need to check everyday all these files are existing or not (irrespective of dates)

I am following the below approach, but some how I feel there could be much better way.

filecount=`ls dir1|grep -c "fileone"*`
if [ filecount -gt 0]; then
 echo "fileone ixists"
fi

like this I am writing for all 3 files.

Is there any other way something I can get the same result?
(may be using find command?)

please suggest

You don't need to use find, ls, grep, or globbing at all. Your use of grep is wrong anyway since grep uses regular expressions, not globbing -- "fileone*" matches "fileon", then one or more e's. and does so anywhere in the line, so "aaaaaafileon" would match "fileone*".

The shell has many operators for use inside [ ] which can be used to check files, directories, or variables in many ways, see man test.

# Today's date
DATE=$(date +%Y_%m_%d)

for X in fileone filetwo filethree
do
        [ -f "${X}_${DATE}.dat" ] && echo "${X} exists"
done

thanks for your inputs. I will have only today'sfiles only, in other words everday the old files will be archived and only current day files will be present in that dir.

so, as per your code, it should be

[ -f "${X}*.dat" ] && echo "${X} exists"

is it correct?

No. The code as I gave it is what I meant. No more, no less.

# Today's date
DATE=$(date +%Y_%m_%d)

for X in fileone filetwo filethree
do
        [ -f "${X}_${DATE}.dat" ] && echo "${X} exists"
done

Your version won't work. * won't expand inside "", and if you remove the "", it will start complaining when * matches more than one file because -f doesn't take multiple files.

If you found a way to make it work, your error-checking code still wouldn't see any difference between files from previous days and files from the current day, making it fairly pointless.

Another Shell way:

YMD=$(date +%Y_%m_%d)
#
if [ -f "fileone_${YMD}.dat" -a -f "filetwo_${YMD}.dat" -a -f "filethree_${YMD}.dat" ]
then
        echo "All three files exist
else
        echo "One of more files is missing"
fi

If I read the question another way, the match on today's date doesn't matter.

filecount=$(ls -1d fileone_????_??_??.dat filetwo_????_??_??.dat filethree_????_??_??.dat 2>/dev/null | wc -l)
if [ $filecount -ne 3 ]
then
        echo "one or more of the three files is missing"
fi