Valhalla Legends Forums Archive | General Discussion | Finding within files in Linux...

AuthorMessageTime
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?
January 5, 2004, 6:48 PM
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.
January 5, 2004, 7:07 PM
iago
I found the file the hard way, but I'm sure this will come up again. Thanks! :)
January 5, 2004, 7:41 PM
Thing
I'm pretty sure it's grep --files-with-matches
Easy huh?
January 5, 2004, 9:45 PM
iago
How do you tell it to do it recursively? Most commands have -R, but I don't think grep does..
January 6, 2004, 1:14 AM
Adron
-r, --recursive equivalent to --directories=recurse.
January 6, 2004, 10:13 AM
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.[/quote]
hmm, I have no idea how I missed that one. Oh well, thanks :)
January 6, 2004, 11:26 AM
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.
January 6, 2004, 2:25 PM
iago
Mine is 2.5. I can't paste the whole thing because my Laptop isn't hooked up to the network right now :)
January 6, 2004, 2:36 PM
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 :/
January 6, 2004, 5:05 PM
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?
January 6, 2004, 5:36 PM
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. :/
January 6, 2004, 5:59 PM
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.
January 6, 2004, 6:16 PM
cipher
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.
January 7, 2004, 2:59 AM
Kp
[quote author=cipher link=board=2;threadid=4608;start=0#msg38612 date=1073444389]
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 }=)[/quote]

That will work, and will list the files which match a pattern. Iago wanted to know which files contained a pattern, though.
January 7, 2004, 6:19 AM
iago
[quote author=cipher link=board=2;threadid=4608;start=0#msg38612 date=1073444389]
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.
[/quote]
I don't know where you got DOS from, I'm using linux. And you can't pipe into grep because grep doexn't read from stdin, it reads from a parameter.
January 7, 2004, 11:52 AM
Adron
[quote author=cipher link=board=2;threadid=4608;start=0#msg38612 date=1073444389]
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 }=)
[/quote]

That's not a very good solution as it won't tell you in *what* directory it found the file. If you're looking for a file, your answer probably won't enlighten you very much at all. I suppose it might give you the answer if you just wonder whether you have a file or not, but finding out where it is would be a logical next step.

Try find <dir> -name <file> instead...
January 7, 2004, 5:00 PM
Kp
[quote author=iago link=board=2;threadid=4608;start=15#msg38646 date=1073476332]I don't know where you got DOS from, I'm using linux. And you can't pipe into grep because grep doexn't read from stdin, it reads from a parameter. [/quote]

Well, Adron's version probably runs in a console window (as does the Windows port of grep that I use), and lots of people seem to get confused and think that a console window is 'DOS'. :) You can do what he suggested and it would have a meaningful effect, though not a useful one as Adron pointed out. If you don't tell grep what files to search, it searches stdin. How else would you chain together a series of greps?
January 7, 2004, 6:33 PM
cipher
[quote author=iago link=board=2;threadid=4608;start=15#msg38646 date=1073476332]
[quote author=cipher link=board=2;threadid=4608;start=0#msg38612 date=1073444389]
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.
[/quote]
I don't know where you got DOS from, I'm using linux. And you can't pipe into grep because grep doexn't read from stdin, it reads from a parameter.
[/quote]
When I said grep in DOS i was referring to what Adron had shown, which looked like a port of grep for command line DOS under some Windows * (gotta watch my ass here and be specific, or someone might try to correct me). Now maybe that's just how he has his PS1 variable set - that's a bit weird if so - and it's just made to look like DOS... but from what Kp has said, and since Adron didn't say anything, i'll just assume it was a DOS prompt.
Also, since when does grep not read from STDIN? This is new to me.
And yes, i realize my answer may not have been quite as functional -- iago was not entirely specific as to what exactly he wanted to do. If in fact you want to know the location of the file, and aren't just looking to see if it exists, then the find <dir> -name <file> would work (as Adron said), or just to show you that piping too would work, you could do find <dir> | grep <file> and that would better work for finding a pattern as the former would only match a file specifically called <file> while the latter would catch 'fsdfs<file>fdfdsf' if you wanted it to. edit: the find <dir> | grep <file> would return directories too, so if what you want are only "files" (directories are files too, but special kind of file) then 'find -type f' (for files) would give you what you want. A quick man find would show you the other "types" you can search for.
-cipher
January 7, 2004, 11:45 PM
iago
As far as I know, the find command works like this:
find [pattern] [switches]
one switch is -name "file name" [I can't really give anymore information since I'm in windows right now]

But I'm not talking at all about DOS, so if you mean in dos, it's pretty useless here. As I said in the title, and probably in my first post, I'm using Linux here, not Windows or Dos.
January 8, 2004, 12:26 AM
cipher
I don't think you are correctly reading my replies, or are at all taking the time to read them. I _know_ you aren't talking about DOS, and everything that I've referenced, command-wise, has been for a computer running Linux - assuming for the most part running bash as your shell. If you read the friggen first paragraph I wrote that I was referring to Adron's DOS-like command-prompt and not at all referring to what you were using. I'm sorry if I have not been clear, but I don't think I can make it any clearer.
January 8, 2004, 1:19 AM
Adron
I'm using Windows 2000:

Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corporation

C:\Documents and Settings\Adron>


And the -name switch for find actually takes wildcards, so if what you want is "*file*", just write that...
January 8, 2004, 8:30 AM

Search