Link to home
Start Free TrialLog in
Avatar of CEHJ
CEHJFlag for United Kingdom of Great Britain and Northern Ireland

asked on

grep: *.java: No such file or directory

Can anyone tell me why I get this infuriating error message when I issue this command?

grep -rl implements *.java

Surely I don't need to use an additional command?

I'm using GNU grep 2.4.2
Avatar of willy134
willy134

I believe it is looking for a directory called something.java  why don't you use

grep -rl implements * | grep java

Good Luck
willy

Or try

grep -rl implements *java

presumably you do have some files where the names end in java ?

Matt
Avatar of CEHJ

ASKER

>>presumably you do have some files where the names end in java ?

Of course ;-) btw I've already tired that (I think). I'll try it again to be sure. Why won't a glob work though?

Don't tell me I really *have* to do something like

grep -rl implements `find . -name *.java`

?
Avatar of CEHJ

ASKER

willy134 i missed you comment at first

>>grep -rl implements * | grep java

Is something quite different ;-) And I don't want grep looking in all files
Avatar of CEHJ

ASKER

>>Is something quite different

Let me qualify that. It will have the same *effect* as I'm looking for (although the second re should really be qualified by the line end) but why give the first grep the unnecessary overhead of searching all files?
Try
grep -rl implements */*.java
Avatar of CEHJ

ASKER

That won't cut it - it excludes the current directory
The problem is _where_ the glob is expanded (and who does the expansion... namely the shell)... As you write it, it'll just expand in "." and include anything (files or directories) ending in .java ... You should be using the --include=<glob> instead:
grep -rl --include=\*.java implements .

-- Glenn
Ooops, that was for gnu grep version 2.5.1 ... 2.4.2 doesn't have it, so unless you'd fancy an upgrade, the find/grep combo is it.

-- Glenn
or you can type
set -o noglob
grep with dangerous wildcard*
set +o noglob
does "ls *.java" give you the same message?

(that might answer your question)

Also, I always keep a few one-line scripts to the effect of:

find / -name "$1" -print 2>/dev/null
and
find / -exec grep "$1" {} \; 2>/dev/null

lying around.  I won't swear to the syntax but you get the idea.
Ignore the bonehead (ls) part of my prev post and try;

find . -name "*.java" -exec grep implements {} \; -print

it'll get you into the neighbourhood (if the quotes on the -exec are right)
That "find ... -exec ..." is worse in one respect, and better in one that the "grep ... `find ...`". Worse in the excessive fork/exec, and better sine it avoids any commandline lenght/number of parameters issues. The best on this account would be
find . -name \*.java -print | xargs grep implements
but CEHJ states s/he doesn't want that.... so then the only way would be to upgrade to a gnu grep that will allow the --include= thing (as in 2.5.1).

I think that just officially beat this question to death...:-)

-- Glenn
ASKER CERTIFIED SOLUTION
Avatar of Gns
Gns

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ

ASKER

>>so unless you'd fancy an upgrade

Would that easy enough to do?
This works for me. And it's not long winded.

grep -l implements `find . -name *.java`
.... And it has already been suggested (as not feasible... by CEHJ... and do read my comment on it's merits/non-merits (implied by the find ... -exec ... comment)) jetstream.

CEHJ: Well, that depends a little on distro etc. You could very well just build it from source (grab it from a gnu-mirror... or ftp://ftp.gnu.org/gnu/grep note that only the source rpm is "there" ATM, since they are confirming that noone has ...manipulated the tar-files. I'd guess 2.5 would be fine too. If youbuild it from source, it is often prudent to install to "an alternate location" than the distros "normal location", or removing the grep package altogether before running make install...
Or you might have an "update" for your distro... If it is rpm-based, http://www.rpmfind.net might be a help... or just apt-get for Debian... As I said, it depends:-).

-- Glenn
jet & gns... I guess I'll be tweaking my find scripts this aft.
didn't read all the comments, so please be patient if my suggestion still have been done:

  grep -rl implements *

or:

  grep -rl implements *|grep java
> grep -rl implements *
Too "inclusive" ... Not limited to results in .java files;-)

> grep -rl implements *|grep java
The first comment (by willy134) mention this... You're a man by your word Achim, you really didn't read all the comments:-):-)
Nice seeing you active again though;-).

-- Glenn
dooh, the very first comment unread by me, and I've some more daylight hours than Gns ;-)
Skol