1834497 Members
2926 Online
110067 Solutions
New Discussion

Re: Bug with find?

 
SOLVED
Go to solution
Eric Antunes
Honored Contributor

Bug with find?

Hi all,

When I do:

#find disc2 -name *.sh -exec grep -l EOT {} \;

I get only 1 file:

disc2/.../scripts/job.sh

But I've more scripts in disc2 with the word EOT...

Any idea??

Thanks,

Eric Antunes
Each and every day is a good day to learn.
16 REPLIES 16
Pete Randall
Outstanding Contributor

Re: Bug with find?

Hi Eric,

Does it make any difference if you quote your grep argument:

grep -l 'EOT' {} \;

How about if you use xargs instead:

find disc2 -name *.sh | xargs grep -l 'EOT'


Pete

Pete
Stephen Keane
Honored Contributor
Solution

Re: Bug with find?

Or?

#find disc2 -name "*.sh" -exec grep -l EOT {} \;
CAS_2
Valued Contributor

Re: Bug with find?

Please, post the outputs of following commands:

ll *.sh
ll disc2/*.sh

Eric Antunes
Honored Contributor

Re: Bug with find?

Hi Pete,

Sames results:

# find disc2 -name *.sh -exec grep -l 'EOT' {} \;
disc2/app/oracle/admin/TST/scripts/job.sh
# find disc2 -name *.sh |xargs grep -l 'EOT'
disc2/app/oracle/admin/TST/scripts/job.sh

I'm sure of this because I've lots of db scripts using svrngrl, i. e., with this line:

...
svrmgrl << EOT
...
Each and every day is a good day to learn.
harry d brown jr
Honored Contributor

Re: Bug with find?

Like Stephen Keane pointed out, you have to use quotes around *.sh, and more accurately you should escape out the epriod:

find disc2 -name "*\.sh" -exec grep -l EOT {} \;

live free or die
harry d brown jr
Live Free or Die
CAS_2
Valued Contributor

Re: Bug with find?

I suspect there is a file called job.sh in your directory.
You should run

find disc2 -name '*.sh' -exec grep -l EOT {} \;
Eric Antunes
Honored Contributor

Re: Bug with find?

Stephen and Harry found it!

But why I need to use "*\.sh" or "*.sh"??

Thanks,

Eric
Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Bug with find?

Is there a good manual about this commands?
Each and every day is a good day to learn.
harry d brown jr
Honored Contributor

Re: Bug with find?

Because the SHELL will try to expand the * and the period, looking in your current directory for any matches to any file or directory that ends in "sh", and then it places it into your find command - not what you wanted.

live free or die
harry d brown jr
Live Free or Die
CAS_2
Valued Contributor

Re: Bug with find?

because shell expand file patterns like "*.sh"

Single quotes or double quotes prevent shell filename expansion.

You'll obtain the same result by disabling the shell expansion:

set -f
find disc2 -name *.sh -exec grep -l EOT {} \;
CAS_2
Valued Contributor

Re: Bug with find?

You can read the following paragraph in the sh-posix manual pages:

File Name Generation
Following substitution, each command word is processed as a pattern
for file name expansion unless expansion has been disabled with the
set -f special command.


You thought you ran

find disc2 -name *.sh -exec grep -l EOT {} \;

but after shell filename expansion the real command launched was

find disc2 -name job.sh -exec grep -l EOT {} \;
Eric Antunes
Honored Contributor

Re: Bug with find?

After doing:

#mv job.sh jobe.sh

I get no results:

#find disc2 -name *.sh -exec grep -l 'EOT' {} \;
#

Is this because of job being a reserved word??

Eric
Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Bug with find?

Ok,

I've got it: I have a job.sh script in root's home directory...
Each and every day is a good day to learn.
harry d brown jr
Honored Contributor

Re: Bug with find?

No, it's because there aren't any files that end in "sh", thus your find is executing with this:

find disc2 -name sh -exec grep -l 'EOT' {} \;

You have to pattern matching characters in quotes unless you want them to be expanded by the SHELL.

and a period is a pattern matching character also, so you need to escape it out:

"*\.sh"

live free or die
harry d brown jr
Live Free or Die
harry d brown jr
Honored Contributor

Re: Bug with find?

Sorry, its:

You have to PUT pattern matching characters in quotes unless you want them to be expanded by the SHELL.

live free or die
harry d brown jr
Live Free or Die
Eric Antunes
Honored Contributor

Re: Bug with find?

Many thanks to all!
Each and every day is a good day to learn.