|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 03:27 AM.. Reason: added code tags |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
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 03:25 AM.. Reason: added code tags |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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 03:25 AM.. Reason: added code tags |
|
#4
|
|||
|
|||
|
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 03:26 AM.. Reason: added code tags |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Code:
for f in /abcd/xyz*
do
[ "$f" -nt /abcd/test.txt ] && echo "file f$ found"
doneShell 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
|
|||
|
|||
|
@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 08:57 AM.. |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
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 08:57 AM.. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 03: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 09:41 AM |
| Check File Exists and compare to previous day file script | rbknisely | Shell Programming and Scripting | 3 | 02-07-2008 10:53 AM |
| script to check for a condition inside a file | kiran1112 | Shell Programming and Scripting | 11 | 03-21-2007 09: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 02:26 AM |
|
|