I'm using ubuntu linux, and I use bash from with a terminal emulator every day for many tasks.

I would like to know how to find a string or a substring within a file that is within a particular directory.

If I was knew the file which contained my target substring, I would just cat the file and pipe it through grep, thus:

cat file | grep mysubstring

But in this case, the pesky substring could be anywhere within a known directory.

How do I hunt down my substring ?

link|improve this question
Belongs on superuser.com – Paul R May 2 '10 at 14:06
@paulR (and all the other people voting to close) - this is a shell programming question. Why does it belong on SuperUser? – APC May 2 '10 at 15:06
1  
@APC: its not really shell programming - it's just basic shell usage, and therefore it belongs on superuser.com – Paul R May 2 '10 at 15:09
@PaulR - this just feels like a narrow definition of what constitutes programming. But the numbers appear to agree with you. – APC May 2 '10 at 15:29
@APC: I guess there's always going to be a grey area on the cusp between "programming" and "non-programming", particularly when it comes to things like shells. My "rule of thumb" is that there needs to be some kind of flow of control, so a bash script with a conditional or a loop constitutes programming, but a one-line command doesn't. But I'm sure others will differ with me on this... ;-) – Paul R May 2 '10 at 15:52
show 2 more comments
feedback

migrated from stackoverflow.com May 2 '10 at 16:49

This question came from our site for professional and enthusiast programmers.

6 Answers

Use a shell wildcard:

grep mysubstring *

If you want to search subdirectories, use the -r option to recurse into them:

grep -r myssubstring .
link|improve this answer
or grep -r mysubstring directory – xenoterracide May 5 '10 at 22:49
feedback
find -type f | xargs grep mysubstring

These commands (find, xargs and grep) have lots of options, so you can tune this operation substantially.

link|improve this answer
add -print0 to find and -0 to xargs if your files/directories may have spaces in their names: find -type f -print0 | xargs -0 grep mysubstring – Doug Harris May 3 '10 at 15:41
feedback

say I want to find all the python code files that contain the text "wiki" under the directory "~/projects", here is the script:

grep -lir "wiki" ~/projects/**/*.py

adjust the script to your specific requirements.

link|improve this answer
feedback

If you don't need to do it in batch mode, you could install midnight commander (mc), it can search for strings in files.

link|improve this answer
feedback

No matter how you do it, don't cat files into grep. Your original version of

cat file | grep mysubstring

is more correctly done as

grep mysubstring file
link|improve this answer
although you're right this isn't an actual answer to the question. – xenoterracide May 5 '10 at 22:48
Why are you telling me this? – Andy Lester May 6 '10 at 17:22
feedback

If you want to find all files with a certain String recursive from "current" dir, use:

find . -type f -exec grep -l mysubstring {} \;

(should work on most *nix')

link|improve this answer
feedback

Your Answer

 
or
required, but never shown