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.

Possible Duplicate:
How to Combine find and grep for a complex search? ( GNU/linux, find, grep )

I have a directory hierarchy of files (some of which are plaintext and others are binary).

Is there an easy way to recursively search the hierarchy of files and directories for the names of any files which contain a specified string, and print out a list of "path to file name and line number" if possible?

Basically, what I want is more or less the same as the following, except for perhaps with an option to print line numbers are printed as well:

#!/bin/bash
#
# Recursively search files for pattern specified as argument.
#

pattern="$1"

if [ -z "$pattern" ]; then echo "$0: Unspecified pattern."; exit; fi

searchfiles () {

  for filename in *; do

    if [ -f "$filename" ]; then {

      result=`/bin/grep -e "$pattern" "$filename"`

      if ! [ -z "$result" ]; then echo "File: `pwd`/$filename"; fi

    }

    elif [ -d "$filename" ]; then

      cd "$filename"

      searchfiles

      cd ..

    fi

  done

}

searchfiles

OK, as suggested below, I could use recursive grep, but I only need the filename and line number, as printing its contents could be messy if there are many long long lines which then wrap around the terminal when printed, so here was my solution:

grep -Rn index.php * | sed -e 's/ .*//g'

Regards,

John Goche

share|improve this question

marked as duplicate by RedGrittyBrick, Bob, alex, Renan, Diogo Jul 26 '12 at 14:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
(the answer there is find /path/to/folder/ -print0 | xargs -0 grep -H -n "string") –  RedGrittyBrick Jul 26 '12 at 9:19
    
How does just find not work for you? find -name '*string*' does not do what you need? –  Bernhard Jul 26 '12 at 9:21
1  
How do you want to print a line number if all you're searching for is filenames which contain that string? –  slhck Jul 26 '12 at 9:22
1  
@Bernhard,slhck: I think the question is poorly worded, from his mention of line-number I deduce that John actually wants to match text in the file content not in its name. John: please edit your question to clarify. –  RedGrittyBrick Jul 26 '12 at 9:25
    
I've edited Q to match my interpretation! Please undo my changes if you disagree. (I think the Q is a dup or at least a minor variation of another Q) –  RedGrittyBrick Jul 26 '12 at 9:29
add comment

1 Answer

up vote 3 down vote accepted
grep -Rn "your_string" *

Should do the trick.

share|improve this answer
    
The OP wanted line number, add -n. –  RedGrittyBrick Jul 26 '12 at 9:57
    
Indeed ! Thanks. :) –  Nunus Jul 26 '12 at 10:01
1  
Since you are doing recursive search, just give the base directory as argument instead of *. With *, the shell has to expand it and you will miss hidden files and directories in the base directory. –  Daniel Andersson Jul 26 '12 at 10:03
add comment

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