• Welcome to Valhalla Legends Archive.
 

Finding within files in Linux...

Started by iago, January 05, 2004, 12:48 PM

Previous topic - Next topic

iago

I need to search an entire directory/subdirectories for a file containing a specific phrase or pattern, and list the files.  I know I can do this with grep, but I don't know how.  Can anybody help?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

grep string *

which will list the hits

Try grep --help and you should get the option required to have it only print the file name of the matching file.

iago

I found the file the hard way, but I'm sure this will come up again.  Thanks! :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Thing

I'm pretty sure it's grep --files-with-matches
Easy huh?
That sucking sound you hear is my bandwidth.

iago

How do you tell it to do it recursively?  Most commands have -R, but I don't think grep does..
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

-r, --recursive           equivalent to --directories=recurse.

iago

Really?  hah, I must have missed that.  *rereads man page*

Quote-R, -r, --recursive
             Read  all  files under each directory, recursively; this is equivalent to the -d recurse option.
hmm, I have no idea how I missed that one.  Oh well, thanks :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

Interesting, mine doesn't seem to support the -R option. It's:

C:\Documents and Settings\Adron>grep --version
grep (GNU grep) 2.4.2

Copyright 1988, 1992-1999, 2000 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

iago

Mine is 2.5.  I can't paste the whole thing because my Laptop isn't hooked up to the network right now :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


iago

hmm, on the version of grep that I'm using on my work computer, it doesn't even have -r.  In fact, --version gives me an error.  All I know is the man page was written in 1997 :/
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

Hehe, not much use asking us when you have two versions of grep, both different from ours...

Try doing it with find and xargs?

iago

I've never heard of xargs, I'll look that up after lunch.

And can find search within files?  I don't think it can, at least not on this system. :/
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

find can find the names of all files
xargs can parse out the names and pass them to another program on the command line
grep can search files for strings

Combine those:

find . | xargs grep somestring
find . -print0 | xargs grep -0 somestring

-print0 makes it work for filenames that contain evil characters such as space or similar.

cipher

#13
heh, grep for DOS, didn't know that existed. too bad you can't just do like a ls -lR <dir> | grep <file>. bet there's no piping like that }=)

btw, if you have a working slocate db you can try a 'locate <file>' if it gives you an error about not having generated a database try an updatedb, if that too fails then let me know; i know that there is a way to force it to generate a new db, just can't remember off the top of my head.

Kp

Quote from: cipher on January 06, 2004, 08:59 PM
heh, grep for DOS, didn't know that existed. too bad you can't just do like a ls -lR <dir> | grep <file>. bet there's no piping like that }=)

That will work, and will list the files which match a pattern.  iago wanted to know which files contained a pattern, though.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!