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*
grep hopper.h */***/*.c
work just fine
Many thanks for the (no doubt obvious) answer
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"
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*
grep -r "hopper.h" *.c
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!
Ultimate Linux Newbie Guide [www.linuxnewbieguide.org]
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.
to do this job I think you can use:
grep -r "hopper" . --include=*.c
I love find :D
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.
|All times are GMT -5. The time now is 05:38 PM.|