Recursive search for files that hold some specific text

4

How do I recursively generate a text file which has a list of all files on my server which contain a specific string anywhere in the files?

I know the following command can be used to replace a string recursively

find /var/www -type f -print0 | xargs -0 sed -i 's/old string/new string/g'

I do not want to replace the string, I just want a list of all files which contain the string.

oshirowanen

Posted 2011-01-18T14:31:12.703

Reputation: 1 858

For a windows user I would have suggested to writes a batch file which saves the output to a file and put it in the scheduler . Try whatever is the linux substitute for this process – Shekhar – 2011-01-18T14:36:06.333

Answers

7

Use grep instead of sed:

find /var/www -type f -print0 | xargs -0 grep -i 'old string'

From the way you phrase the question, it seems like you're not yet too familiar with grep. Read more about its options in its man page type: man grep at your command line.

update to answer comment -- try adding the -l option to show just file names. The -i makes the search case insensitive. The easy to use both is with a single dash: grep -il

Doug Harris

Posted 2011-01-18T14:31:12.703

Reputation: 23 578

This does not seem to give me just the paths/files containing the string I am looking for. For some reason, I am also getting the text from the files searched too. – oshirowanen – 2011-01-18T14:49:05.340

1Added bit about -l – Doug Harris – 2011-01-18T15:49:05.733

4

You can also use grep alone without find:

grep -Rli 'old string' /var/www > list_of_files

garyjohn

Posted 2011-01-18T14:31:12.703

Reputation: 29 085

1

You could try adding the -l flag to the command to only list the file names

find /var/www -type f -print0 | xargs -0 grep -li 'old string'

Ben V

Posted 2011-01-18T14:31:12.703

Reputation: 131

0

Try ack -- it's faster than grep and supports perl regex.

ack -la <pattern> /var/www

On some systems this package is called "ack" and sometimes its called "ack-grep".

Mark E. Haase

Posted 2011-01-18T14:31:12.703

Reputation: 3 243