Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to find a string or phrase recursively in all files in a folder from the command line, such that the output would be similar to Sublime Text 2's find behavior? Like, with the path of the filename that the string has been found in and some of the text block that contains it?

share|improve this question
Can you upload an image of "Sublime Text 2's find behavior"? – matan129 Jul 1 '13 at 17:23

I will assume you are using a *nix system. If you just want the files in the current folder, do

$ grep -C 3 foo *
bar.txt-This is line one
bar.txt:This is line two, which contains foo
bar.txt-This is line 3

If you also want to recurse into subfolders, use

grep -rC 3 foo


   grep,  egrep,  fgrep,  rgrep - print lines matching a

  -C NUM, -NUM, --context=NUM
          Print  NUM  lines of output context.  Places a
          line containing a group separator (--) between
          contiguous  groups of matches.  With the -o or
          --only-matching option, this has no effect and
          a warning is given.
   -R, -r, --recursive
          Read   all   files   under   each   directory,
          recursively;  this  is  equivalent  to  the -d
          recurse option.
share|improve this answer
If you have a recent GNU(?) grep, you might want to use also the --color option. The filename is then printed in magenta, the matchting text in red. – mpy Jul 2 '13 at 11:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .