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?

  • 4
    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.
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.