find & grep | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

find & grep

UNIX for Dummies Questions & Answers


Tags
grep or, linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-31-2001
Anika Anika is offline
Registered User
 
Join Date: Jan 2001
Last Activity: 22 July 2002, 8:56 AM EDT
Location: The Netherlands
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question

Hi,

I would like to know which files contain a certain string. If I use 'grep "string" *' only the working directory is being searched. I also want to search the subdirectories. When I use 'find . -type f -print |xargs grep "string" > dev/null' I get the message 'xargs: missing quote?'. What's up?
Should I use another command?

Thanks in advance.

Anika
Sponsored Links
    #2  
Old 01-31-2001
mib mib is offline
Registered User
 
Join Date: Jan 2001
Last Activity: 8 July 2003, 3:54 AM EDT
Location: Calicut
Posts: 228
Thanks: 0
Thanked 7 Times in 4 Posts
grep -r "string" * will read all files under each directory recursively.

Sponsored Links
    #3  
Old 01-31-2001
PxT's Avatar
PxT PxT is offline Forum Advisor  
Registered User
 
Join Date: Oct 2000
Last Activity: 7 May 2009, 12:22 PM EDT
Location: Sacramento, CA
Posts: 909
Thanks: 0
Thanked 4 Times in 4 Posts
Which version of Unix does this work on? I have never encountered a grep that works like this. A portable solution is:

find . -exec grep string {} /dev/null \;
    #4  
Old 01-31-2001
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
 
Join Date: Sep 2000
Last Activity: 22 October 2014, 8:02 PM EDT
Location: Asia pacific region
Posts: 13,023
Thanks: 522
Thanked 877 Times in 402 Posts
The beauty of UNIX is the 10,000 ways to do these things. Here is how I do it (one way) when I want to go down the tree:


Code:
find * | grep string

It is not the typical way, but I know many old UNIX people who do it this way. Guess we are lazy to think or type

For example, if we want to search the entire file system for libc.so.2 we would do this:


Code:
find / | grep libc.so

This way we find all the libc.so files. If we did not care about case then:


Code:
find / | grep  -i libc.so

I also like using egrep with regular expression matching for more tricky or complex matches and searches.



Sponsored Links
    #5  
Old 01-31-2001
PxT's Avatar
PxT PxT is offline Forum Advisor  
Registered User
 
Join Date: Oct 2000
Last Activity: 7 May 2009, 12:22 PM EDT
Location: Sacramento, CA
Posts: 909
Thanks: 0
Thanked 4 Times in 4 Posts
However, this will only find you the filenames that contain the string. It will not search for the string <I>within</I> the file, which is what the original poster was asking about.
Sponsored Links
    #6  
Old 01-31-2001
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
 
Join Date: Sep 2000
Last Activity: 22 October 2014, 8:02 PM EDT
Location: Asia pacific region
Posts: 13,023
Thanks: 522
Thanked 877 Times in 402 Posts
Doing a find and then greping will certainly find the string within a string (in the file name) and works much better and faster. You are right PxT, this will not search the string within the file.


Code:
find / | grep lib

You will find a zillion occurances of the string 'lib' within a string. Here is a small sample and I've not even refined the grep or egrep (and just captured small output).

/usr/lib/gdk-pixbuf/loaders/libpixbufloader-png.so.1.0.0
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-pnm.a
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-pnm.la
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-pnm.so.1.0.0
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-ras.a
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-ras.la
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-ras.so.1.0.0
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-tiff.a
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-tiff.la
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-tiff.so.1.0.0
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-xpm.a
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-xpm.la
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-xpm.so.1.0.0
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-bmp.so
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-bmp.so.1
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-gif.so
/usr/lib/gdk-pixbuf/loaders/libpixbufloader-gif.so.1

Yes, it does not search the actual file. I normally use PERL for that and not xargs. For some strange reason, xargs and I don't gel.... when I have to search files and strings within lots of files I use command line PERL.

[Edited by Neo on 01-31-2001 at 08:52 PM]
Sponsored Links
    #7  
Old 02-01-2001
mib mib is offline
Registered User
 
Join Date: Jan 2001
Last Activity: 8 July 2003, 3:54 AM EDT
Location: Calicut
Posts: 228
Thanks: 0
Thanked 7 Times in 4 Posts
Quote:
Originally posted by PxT
Which version of Unix does this work on? I have never encountered a grep that works like this. A portable solution is:

find . -exec grep string {} /dev/null \;


grep -r "string" * works pretty well on Linux(RH).

However this is not a comprehensive method. But I think this could be used for what poster is mentioned since he is using '*'(this cause grep read all files under each directory recursively).

But if he uses grep -r "www" html* will only recurse into subdirectories whose names starts with "html". so if there is a subdirectory whose name is not starts with "html" will be ignored even if it has the file that contain "www" string.

my grep version: GNU grep 2.3

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block lifechamp UNIX for Dummies Questions & Answers 0 03-12-2012 10:01 AM
How to use grep & find command to find references to a particular file Gangam Shell Programming and Scripting 2 09-22-2011 03:52 AM
grep or find ? DallasT UNIX for Dummies Questions & Answers 5 07-19-2011 03:50 PM
find , grep james94538 UNIX for Dummies Questions & Answers 3 10-09-2008 09:03 PM
grep, find or awk? netrom UNIX for Dummies Questions & Answers 4 04-09-2008 05:03 PM



All times are GMT -4. The time now is 09:39 PM.