If condition to check one file newer than the other - first file name uncertain | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-06-2009
achilles5 achilles5 is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 8 October 2009, 6:37 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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.

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

Last edited by Yogesh Sawant; 07-10-2009 at 04:27 AM.. Reason: added code tags
Sponsored Links
    #2  
Old 07-06-2009
Ramesh_srk Ramesh_srk is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 16 July 2009, 4:47 AM EDT
Location: Chennai,India
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Try like this:
Code:
if [ `ls -1 /abcd/xyz*` -nt /abcd/test.txt ]
then
echo "file found"
else
echo "file not found"
fi


Last edited by Yogesh Sawant; 07-10-2009 at 04:25 AM.. Reason: added code tags
Sponsored Links
    #3  
Old 07-06-2009
achilles5 achilles5 is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 8 October 2009, 6:37 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Still not working

Hi Ramesh,

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

Code:
0403-012 A test command parameter is not valid.

Thanks

Last edited by Yogesh Sawant; 07-10-2009 at 04:25 AM.. Reason: added code tags
    #4  
Old 07-06-2009
Ramesh_srk Ramesh_srk is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 16 July 2009, 4:47 AM EDT
Location: Chennai,India
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Check whether this will return only 1 file or more than 1 files

Code:
ls -1 /abcd/xyz*


Last edited by Yogesh Sawant; 07-10-2009 at 04:26 AM.. Reason: added code tags
Sponsored Links
    #5  
Old 07-06-2009
kshji's Avatar
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 26 February 2014, 5:44 AM EST
Location: Finland
Posts: 602
Thanks: 0
Thanked 50 Times in 49 Posts

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

Code:
echo rm -r -i  /abcd/xyz*

If echoed line looks good, then run the cmd.
Sponsored Links
    #6  
Old 07-07-2009
achilles5 achilles5 is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 8 October 2009, 6:37 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
@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

Last edited by achilles5; 07-07-2009 at 09:57 AM..
Sponsored Links
    #7  
Old 07-07-2009
achilles5 achilles5 is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 8 October 2009, 6:37 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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

Last edited by achilles5; 07-07-2009 at 09:57 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to parse through a file and based on condition form another output file sivasu.india UNIX for Advanced & Expert Users 6 02-28-2008 04:59 AM
Changing old file name to new one if the old file name satisfied certain condition balzzz UNIX for Dummies Questions & Answers 1 02-17-2008 10:41 AM
Check File Exists and compare to previous day file script rbknisely Shell Programming and Scripting 3 02-07-2008 11:53 AM
script to check for a condition inside a file kiran1112 Shell Programming and Scripting 11 03-21-2007 10:38 AM
Have a shell script check for a file to exist before processing another file heprox Shell Programming and Scripting 3 11-14-2006 03:26 AM



All times are GMT -4. The time now is 12:09 AM.