Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I stop the FIND command from Recursivly Searching

Status
Not open for further replies.

pepito609

Programmer
Joined
Nov 4, 2003
Messages
12
Location
US
Hi guys,
Sorry about the really simple question, but I don't use UNIX much, I am writing a script that executes some other script code based on whether it finds some files or not. Here is what I came up with:

#!/bin/csh
FILE='XXX.YYY.*'
if find ./ -name $FILE ; then
echo "true"
else
echo "false"
fi

The problem is that in another sub-directory I have historical files with the same name structure with ".gz" I have to use the * wildcard and as such it picks up the ".gz" in the other subdirectory. Also, the names can not change.

Is there a way for me to tell the find command to not search recursively?

Thanks and my apologies for the simplicity of the question.

pepito609
 
Don't use [tt]find[/tt]. If you're just looking in the current directory, an [tt]ls[/tt] will do.

I don't know C-shell too well, but here it is in Korn shell...
Code:
#!/bin/ksh

FILE='XXX.YYY.*'

ls ${FILE} > /dev/null 2>&1 ; STATUS=!$?

if (( ${STATUS} ))
then
    echo "True, ${FILE} exists in $(pwd)!"
else
    echo "False, ${FILE} doesn't exist in $(pwd)!"
fi
Hope this helps.
 
FYI a find command can also "exclude" files by using:-
! -name "$EXCLUDEFILE" within the command

EXCLUDEFILE="*.gz"
find $DIRECTORY -name "$FILENAME" ! -name "$EXCLUDEFILE" -mtime $DAYS -type f -exec $ACTION {} \; $LOG
 
You can also do a

if [ -a $FILE ]
then
echo $FILE exists
else
echo $FILE does not exist
fi
 
Also the -prune option is useful for controlling find's level of recursion.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top