LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-03-2005, 04:13 PM   #1
robgee1964
LQ Newbie
 
Registered: Nov 2005
Posts: 2

Rep: Reputation: 0
Question 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
 
Old 12-03-2005, 04:24 PM   #2
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
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"
 
Old 12-03-2005, 04:27 PM   #3
gnu2tux
Member
 
Registered: Nov 2005
Location: Scotland, UK
Distribution: Ubuntu
Posts: 32

Rep: Reputation: 15
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]
 
Old 12-03-2005, 05:14 PM   #4
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Ruhr Area, Germany
Distribution: Slackware64 14.0
Posts: 1,517

Rep: Reputation: 90
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.
 
Old 12-03-2005, 07:14 PM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,008
Blog Entries: 5

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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.
 
Old 12-04-2005, 04:58 PM   #6
robgee1964
LQ Newbie
 
Registered: Nov 2005
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
Old 12-04-2005, 05:11 PM   #7
giallu
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Rep: Reputation: 0
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
 
Old 12-04-2005, 05:20 PM   #8
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Ruhr Area, Germany
Distribution: Slackware64 14.0
Posts: 1,517

Rep: Reputation: 90
I love find

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" {} \;

Last edited by titopoquito; 12-04-2005 at 05:23 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling wildcards... artificialGekko Linux - General 5 03-24-2005 08:29 PM
CLI wildcards ShakyJake Linux - Newbie 4 02-28-2005 03:42 PM
Wildcards dazdaz Linux - Newbie 3 01-23-2005 05:33 AM
Wildcards in kernel Squall Linux - General 1 04-20-2004 05:22 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM


All times are GMT -5. The time now is 03:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration