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.
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:
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.
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