Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
4  
Can you upload an image of "Sublime Text 2's find behavior"? –  matan129 Jul 1 '13 at 17:23
add comment

1 Answer

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

Details:

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


  -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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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