LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Use of wildcards and -R switch in ls and grep (http://www.linuxquestions.org/questions/linux-newbie-8/use-of-wildcards-and-r-switch-in-ls-and-grep-388936/)

robgee1964 12-03-2005 04:13 PM

Use of wildcards and -R switch in ls and grep
 
Hi folks, totally basic and trivial question here.

I'm totally new to Linux, but have worked with Unix before, on both Sun and HP workstations.

My question is this

1) How do I use ls to in conjunction with wildcards to list a particular file in the entire subdirectory tree? (not just the immediate subdirs)

For example, I'm looking for all files beginining with f in all subdirectories (not just the immediate ones)

My memory is maybe playing tricks but I'm sure

ls -r f* would have worked in my Sun and HP days

2) I've got a simular problem with grep, it doesn't seem to like working with recursive subdirectories either

For example I want to find the instances of "hopper.h" in all files ending .C

I thought the following should work :-
grep -r hopper.h *.c

But again, it doesn't seem to like my use of wildcard, and I get the message
grep: *.c: No such file or directory


Interestingly, I can get the *** directory path wildcard to work

So ls */***/f*

and

grep hopper.h */***/*.c

work just fine


Many thanks for the (no doubt obvious) answer

Regards

Rob

Artanicus 12-03-2005 04:24 PM

I know why it doesn't work, but am very short on ideas how to make it work. Bash (and other shells maybe too) will convert the * wildcard to the full dir listing of the current directory. (In most cases that is) ..

Ofcourse you can do ls */*/*/*.log etc, but then you have to know the deepness your looking for.. I mostly just use find for jobs like that.. find ./ -name "*.cpp"

gnu2tux 12-03-2005 04:27 PM

Hi Rob,

Quote:

Originally Posted by robgee1964
Hi folks, totally basic and trivial question here.

1) How do I use ls to in conjunction with wildcards to list a particular file in the entire subdirectory tree? (not just the immediate subdirs)

For example, I'm looking for all files beginining with f in all subdirectories (not just the immediate ones)

My memory is maybe playing tricks but I'm sure

ls -r f* would have worked in my Sun and HP days

You are almost right. I think perhaps your memory is failing you! - Depending upon the HP system you were on, certainly Tru64, doesn't even have a recursive mode in ls!

You want this:

ls -R |grep f

although grepping for f is a bit useless, i'd grep some more, eg:

ls -R |grep -i file*

Quote:

Originally Posted by robgee1964
2) I've got a simular problem with grep, it doesn't seem to like working with recursive subdirectories either

For example I want to find the instances of "hopper.h" in all files ending .C

I thought the following should work :-
grep -r hopper.h *.c

But again, it doesn't seem to like my use of wildcard, and I get the message
grep: *.c: No such file or directory

Regards

Rob

As you want a literal search of 'hopper.h', you need to encapsulate it in quotes, eg:

grep -r "hopper.h" *.c

that's all!

PS: Just in case you were wondering, I don't know why the gnu people decicded that Recursive in ls is -R, but -r in grep, but that's the way it is!


HTH,

Ali Ross
Webmaster,
Ultimate Linux Newbie Guide [www.linuxnewbieguide.org]

titopoquito 12-03-2005 05:14 PM

Quote:

Originally Posted by robgee1964
1) How do I use ls to in conjunction with wildcards to list a particular file in the entire subdirectory tree? (not just the immediate subdirs)

For example, I'm looking for all files beginining with f in all subdirectories (not just the immediate ones)

My memory is maybe playing tricks but I'm sure

ls -r f* would have worked in my Sun and HP days

Code:

find . -type f -iname "f*"
or -name instead of -iname if you don't want to ignore case.

MensaWater 12-03-2005 07:14 PM

Having been a professional Unix System Admin for both Solaris and HP-UX among dozens of variants I can tell you the ls syntax wouldn't have been any better on Unix than Linux.

Also recursive is upper case R not lower case (ls -R).

You could do "ls -R |grep ^f" it would give you all files that started with f but not the paths they were in. Doing "ls -R |egrep ":|^f" would give you all directory entries then all file names that started with f but they wouldn't be on the same line.

Therefore the BEST way to do it is find as mentioned by another poster.

grep matches on a pattern that may or may not be embedded in longer strings so wild cards are a little meaningless to it though I've seen some versions that allow for it I've never seen them on Unix.

robgee1964 12-04-2005 04:58 PM

Quote:

Originally Posted by jlightner
Having been a professional Unix System Admin for both Solaris and HP-UX among dozens of variants I can tell you the ls syntax wouldn't have been any better on Unix than Linux.

Also recursive is upper case R not lower case (ls -R).

You could do "ls -R |grep ^f" it would give you all files that started with f but not the paths they were in. Doing "ls -R |egrep ":|^f" would give you all directory entries then all file names that started with f but they wouldn't be on the same line.

Therefore the BEST way to do it is find as mentioned by another poster.

grep matches on a pattern that may or may not be embedded in longer strings so wild cards are a little meaningless to it though I've seen some versions that allow for it I've never seen them on Unix.


Thanks for all your replies, I'll use find for the recursive directory thing.

As for grep, what I was meaning is how would I use grep to search recursively? Using the -r switch I thought? Incidentally I've got the wild card in the search string, not the filespec.

Putting the search string in quotes doesn't seem to help, I still can't get it to search recursively.

eg grep -r "hopper" *.c just comes up with
grep: *.c: no such file or directory.

giallu 12-04-2005 05:11 PM

Quote:

Putting the search string in quotes doesn't seem to help, I still can't get it to search recursively.

eg grep -r "hopper" *.c just comes up with
grep: *.c: no such file or directory.
The other comment was true: *.c is expanded by the shell _before_ running the command resulting in the error message.

to do this job I think you can use:

grep -r "hopper" . --include=*.c

titopoquito 12-04-2005 05:20 PM

I love find :D

Code:

find . -type f -iname "*.c" -exec grep "hopper" {} \;
You can also use xargs, search here on LQ if you are interested.

EDIT: I forgot to mention that you could use grep with an -l switch. If you do this you will get all files listed which contain the search string, but only the filename.

Code:

find . -type f -iname "*.c" -exec grep -l "hopper" {} \;


All times are GMT -5. The time now is 08:17 PM.