Skip to content
Advertisement

How to find all files on a *NIX server containing a certain string

I want to produce a list of every file on my server that contains a certain email address within the file. Starting with htdocs and all of its sub-directories.

I am using this command:

find /htdocs/ | xargs grep -n "myname@abc.com"

I seem to be getting the files that contain the string, but I am alos getting a listing of all directories, which I do not want in the list. I simply want the file containg the stirng to be returned.

Here are some of the results I do not want to see:

  • grep: /htdocs/live/abcdef/contact/facilities/GoogleCalendar/gphp/examples/templates: Is a directory
  • grep: edited.jpg: No such file or directory
  • grep: /htdocs/.pki/nssdb: Permission denied

Advertisement

Answer

grep can recurse on a given directory if you use the -r or -R flag. The second one will follow all symbolic links. The rgrep is a shortcut for grep -r.

grep -nsF 'myname@abc.com' -R /htdocs

-F treats the pattern as a fixed string, and -s silences the output in case of unreadable or nonexisting files.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement