Grep files in subdirectories

Asked by Dave

I have ubuntu 11.04
On windows systems I can use find in files to look for my strings in my source (*.c) files
In dos prompts I can use grep to do the same

If I try grep on ubuntu in the form
grep -iaR "string" *.c
I am told that *.c doesn't exist AND IT DOES!!!!!!! SEVERAL OF THEM!!!!!!

I've seen several places on the net all claiming that grep works. Clearly it doesn't on all systems. Why is it that Unix doesn't appear to be able to successfully do what unix, solaris, dos, cpm, windows... in fact just about any operating system since man stopped chisseling messages in stone can manage? So far this is one OS I would not recommend to my worst enemy never mind a friend.

Also why the stupid rules on what I must use as a password to what is only a discussion site? This isn't a world renowned hyper secure nuclear facility its a place to ask questions about an OS that blatently doesn't work properly.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu grep Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
mycae (mycae) said :
#1

>grep -iaR "string" *.c
It works for me on my ubuntu system; are you in the correct working directory

>Also why the stupid rules on what I must use as a password to what is only a discussion site?
1) So that, for example someone can't use my account to suggest that you run a command that destroys your computer. If you look through my account, you can see that I have, (i hope), been helpful, and this might convey trust. Allowing abuse of that trust by masquerading would be a terrible design.
2) To prevent spam bots from posting under my or your account. Thus it becomes easy to mop up spam by deleting a spam user, and all associated posts.

Revision history for this message
mycae (mycae) said :
#2

P.s. Not that command will only find files in the current folder ie:

user@hostname:~$

would indicate you are in directory "~" (which is your home folder, eg /home/user/).

user@hostname:~/SomeFolder$

would indicate you are in /home/user/SomeFolder

Note that if there are files in SomeFolder, and none in /home/user/ and you do this

user@hostname:~$ grep -iaR "string" *.c

you will get no result.

You would need to do something like so:

user@hostname:~$ cd SomeFolder
user@hostname:~/SomeFolder/$ grep -iaR "string" *.c
Blah.c: char *myString;

Revision history for this message
Ralph Corderoy (ralph-inputplus) said :
#3

grep 'pattern' *.c

should suffice for the normal use. You're using -R and *.c, that's almost certainly wrong in most cases, please read the output of `man grep'. Perhaps your C files are from Windows and are *.C? Does

ls *.c

show the files? How about

ls | grep -i '\.c$'

If you want to use -r/-R then do

grep -r 'pattern' .

to search all files, not just ones ending in .c.

Revision history for this message
Dave (d-hitchman) said :
#4

The files are from linux and are .c not .C (though why anyone should write an OS that thinks they are different I never did fathom) The grep 'pattern' *.c doesn't (even according to the manual) do a search through sub directories, and frankly digging through each of maybe 30 or 40 directories in 4 or 5 layers really doesn't appeal - surely the computer is best equipped to do that not a human? I may look like a monkey but I prefer not to act like one.
It appears from other answers that for some reason the shell decides to interpret the *.c and rather stupidly doesn't do anything about looking at ALL the arguments - and totally blanks the look in all sub directory ones.
Given this command DOES function elsewhere it seems stupid to have got such a simple command so wrong.
Oh well, I live and learn, why provide a sensible command when you can provide endless amusement with pipes, execs and 50 character command line prompts that are vastly different from any other form of language.
Sometimes I really do wish Microsoft had taken over the world and killed every other OS stone dead.

----- Original Message -----
From: Ralph Corderoy <email address hidden>
To: d hitchman <email address hidden>
Sent: Wed, 08 Feb 2012 22:06:01 -0000 (UTC)
Subject: Re: [Question #187160]: Grep files in subdirectories

Your question #187160 on grep in Ubuntu changed:
https://answers.launchpad.net/ubuntu/+source/grep/+question/187160

Ralph Corderoy proposed the following answer:
grep 'pattern' *.c

should suffice for the normal use. You're using -R and *.c, that's
almost certainly wrong in most cases, please read the output of `man
grep'. Perhaps your C files are from Windows and are *.C? Does

ls *.c

show the files? How about

ls | grep -i '\.c$'

If you want to use -r/-R then do

grep -r 'pattern' .

to search all files, not just ones ending in .c.

--
If this answers your question, please go to the following page to let us
know that it is solved:
https://answers.launchpad.net/ubuntu/+source/grep/+question/187160/+confirm?answer_id=2

If you still need help, you can reply to this email or go to the
following page to enter your feedback:
https://answers.launchpad.net/ubuntu/+source/grep/+question/187160

You received this question notification because you asked the question.

Revision history for this message
Dave (d-hitchman) said :
#5

I noticed that, why though when I specifically say to search sub directories and I have the *.c files in sub directories under my home folder do I still get nothing? According to other answers its because the shell decides to interpret the *.c and ignores the other parameters rather than just passing the whole damned lot to a grep program that might work.

----- Original Message -----
From: mycae <email address hidden>
To: d hitchman <email address hidden>
Sent: Wed, 08 Feb 2012 19:45:43 -0000 (UTC)
Subject: Re: [Question #187160]: Grep files in subdirectories

Your question #187160 on openoffice.org in Ubuntu changed:
https://answers.launchpad.net/ubuntu/+source/openoffice.org/+question/187160

mycae proposed the following answer:
P.s. Not that command will only find files in the current folder ie:

user@hostname:~$

would indicate you are in directory "~" (which is your home folder, eg
/home/user/).

user@hostname:~/SomeFolder$

would indicate you are in /home/user/SomeFolder

Note that if there are files in SomeFolder, and none in /home/user/ and
you do this

user@hostname:~$ grep -iaR "string" *.c

you will get no result.

You would need to do something like so:

user@hostname:~$ cd SomeFolder
user@hostname:~/SomeFolder/$ grep -iaR "string" *.c
Blah.c: char *myString;

--
If this answers your question, please go to the following page to let us
know that it is solved:
https://answers.launchpad.net/ubuntu/+source/openoffice.org/+question/187160/+confirm?answer_id=1

If you still need help, you can reply to this email or go to the
following page to enter your feedback:
https://answers.launchpad.net/ubuntu/+source/openoffice.org/+question/187160

You received this question notification because you asked the question.

Revision history for this message
Dave (d-hitchman) said :
#6

The whole point is that I want to search all the sub directories WITHOUT changing to them one at a time, after all I'm a human not a computer.
----- Original Message -----
From: mycae <email address hidden>
To: d hitchman <email address hidden>
Sent: Wed, 08 Feb 2012 19:40:58 -0000 (UTC)
Subject: Re: [Question #187160]: Grep files in subdirectories

Your question #187160 on openoffice.org in Ubuntu changed:
https://answers.launchpad.net/ubuntu/+source/openoffice.org/+question/187160

    Status: Open => Answered

mycae proposed the following answer:
>grep -iaR "string" *.c
It works for me on my ubuntu system; are you in the correct working directory

>Also why the stupid rules on what I must use as a password to what is only a discussion site?
1) So that, for example someone can't use my account to suggest that you run a command that destroys your computer. If you look through my account, you can see that I have, (i hope), been helpful, and this might convey trust. Allowing abuse of that trust by masquerading would be a terrible design.
2) To prevent spam bots from posting under my or your account. Thus it becomes easy to mop up spam by deleting a spam user, and all associated posts.

--
If this answers your question, please go to the following page to let us
know that it is solved:
https://answers.launchpad.net/ubuntu/+source/openoffice.org/+question/187160/+confirm?answer_id=0

If you still need help, you can reply to this email or go to the
following page to enter your feedback:
https://answers.launchpad.net/ubuntu/+source/openoffice.org/+question/187160

You received this question notification because you asked the question.

Revision history for this message
mycae (mycae) said :
#7

Then use a recursive tool like find.

find ./ -name *.c -exec grep "string" {} \;

If you want people to help you, please state your case simply - we don't care if you like the OS or not - we are volunteers and are choosing to be helpful. I considered not answering your question due to the way you phrased it.

Revision history for this message
Ralph Corderoy (ralph-inputplus) said :
#8

> > > If I try grep on ubuntu in the form
> > > grep -iaR "string" *.c
> > > I am told that *.c doesn't exist AND IT DOES!!!!!!! SEVERAL OF
> > > THEM!!!!!!
> >
> > grep 'pattern' *.c
> >
> > should suffice for the normal use. You're using -R and *.c, that's
> > almost certainly wrong in most cases, please read the output of `man
> > grep'. Perhaps your C files are from Windows and are *.C? Does
> >
> > ls *.c
> >
> > show the files? How about
> >
> > ls | grep -i '\.c$'
> >
> > If you want to use -r/-R then do
> >
> > grep -r 'pattern' .
> >
> > to search all files, not just ones ending in .c.
>
> The grep 'pattern' *.c doesn't (even according to the manual) do a
> search through sub directories

I never suggested it did. And you never asked us for a command that did
search recursively. You gave one example, passing grep -R for recursive
but at the same time specifying *.c in the current directory as the
files to search. So the command was confused, "certainly wrong" as I
said above, and the commentary lacking but stressed the *.c files were
important.

The second command I gave, "grep -r 'pattern' .", does search
recursively from the current directory downwards, through all files, for
the given pattern.

> I may look like a monkey but I prefer not to act like one.

Sound advice. :-)

> It appears from other answers that for some reason the shell decides
> to interpret the *.c

Correct, and it does so because it's not the task of every program to do
this work. Instead, if the shell improves the `globbing' it provides
then all commands benefit. (Originally, globbing wasn't built into the
shell but a separate glob(1) program.)

> and rather stupidly doesn't do anything about looking at ALL the
> arguments - and totally blanks the look in all sub directory ones.

This makes no sense. Unix seems fundamentally at odds with the
Microsoft model you've grown up with. Switching to it needs an open
mind and patience. Plenty will volunteer their help but only if they
think you've already tried to help yourself and aren't insulting what
they like.

> Sometimes I really do wish Microsoft had taken over the world and
> killed every other OS stone dead.

If you want a Microsoft OS you know where to find one. :-) Many have
been very happy for decades with Unix and its copies and you could be
too, just relax and realise that switching from the closeted, weak, MS
command line model to Unix's might take some time and be initially
frustrating.

Revision history for this message
Ralph Corderoy (ralph-inputplus) said :
#9

mycae wrote:
> find ./ -name *.c -exec grep "string" {} \;

The *.c needs quoting to stop the shell globbing it; we want find(1) to do the globbing as it descends.

    find ./ -name '*.c' -exec grep 'pattern' {} \;

GNU find defaults to starting at . so that could be omitted. The -exec will run grep once for each .c file found, and grep will print out lines matching pattern but won't print the filename because it only does that if multiple files are given or -H is specified.

A better way is to have GNU find queue up the .c file names until there's enough to run grep with a saturated argument list, less greps will be run and the fork/exec overhead will be cut. A -H (or /dev/null) is still required because only one .c file may be found or the last one found may be given to grep on its own.

    find -name '*.c' -exec grep -H 'pattern' {} +

Or grep's --include can be used to limit its -r recursive search to *.c files.

Can you help with this problem?

Provide an answer of your own, or ask Dave for more information if necessary.

To post a message you must log in.