If condition to check one file newer than the other - first file name uncertain

Dear All,

I'm new to unix scripting. I'm trying to write an utility script which has to check if one file is newer than another one. The condition is I dont know the full name of the first file. I searched google and this forum for any such examples, but couldn't find any or may be I would have missed it.

I wrote this simple check using if, but it is not working.

if [ /abcd/xyz* -nt /abcd/test.txt ]
then
echo "file found"
else
echo "file not found"

If i give a definite file name, it prints "file found", but when I use an *, even if the file is present it says "file not found"

I will have to use the wildcard charcater * bcoz each time the text following "xyz" is different.

Could anyone please tell me how to fix this.

Your help is much appreciated.

Thanks

Try like this:

if [ `ls -1 /abcd/xyz*` -nt /abcd/test.txt ]
then
echo "file found"
else
echo "file not found"
fi

Hi Ramesh,

Thanks for a quick reply. Now I'm getting the following error message when I run the script.

0403-012 A test command parameter is not valid.

Thanks

Check whether this will return only 1 file or more than 1 files

ls -1 /abcd/xyz*
for f in /abcd/xyz*
do
     [ "$f" -nt /abcd/test.txt ] && echo "file f$ found"
done

Shell handle variables, filegeneration, ... and after shell has expanded those, it will do the command line. Shell will do this for every line. If is also commandline.
When you are using filegeneraton (*, ?, ...), use echo to test. Ex:

echo rm -r -i  /abcd/xyz*

If echoed line looks good, then run the cmd.

@Ramesh_srk

Yes Ramesh the directory has a lot of files having xyz* files.

@kshji

Thanks a lot. Its working as expected.

Just wanted to clarify one more thing. There are more than 100 xyz* files present in the directory and I'm checking for only today's file which will be one or two and the others are old files. If I use a for loop will it not take more time?

coz I have search for a lot of different files in different directories created for the day. All these directories will have 100 of files with the same starting charcters as the ones I search for. So if I use a "for" loop wont the script take more time and more CPU utilization. Just wanted to know.

In between your explanation of unix treats the variables and conditions is very helpful. Thanks a ton for that too.

Thanks

I forgot to ask one more thing. If I use a for loop and if the file is not found how would I be able to say that the file not found explicitly as I do in if statement

Thanks a lot for your help guys

for f in /abcd/xyz*
do
    if [ "$f" -nt /abcd/test.txt ] ; then
             echo "file $f found"
    else
             echo "file $f not found"
    fi
done

Or

for f in /abcd/xyz*
do
     msg="file $f not found"
     [ "$f" -nt /abcd/test.txt ] && echo "file $f found" && continue
     echo "$msg"
done

Thanks Again kshji. You are cool :b::slight_smile:

I know my doubts were dumb, thanks for your patience to too :smiley:

if file not found then it will result to else statement