start blog post

Recursive Find-in-Files on Linux or Unix

I find myself needing this more and more often lately, so I figured this is as good a place as any to keep track of it. Here's how to recursively search for files containing a particular string or regular expression. This should work on most Linux or UNIX systems, depending on your configuration.

    find -type f -name "*" | xargs grep 'word, phrase, or regular expression'

Summary for the curious:

  • find returns the names of one or more files, but not their contents.
  • -type f tells find to only look at files, not directories.
  • -name "*" tells find to examine everything. You can replace this with "*.js" to only search JavaScript files, for example.
  • | "pipes" the output of the command on the left to the input of the command on the right.
  • xargs creates and executes a command based on the arguments you pass to it.
  • grep searches the contents of files and returns each line that is a match.
  • '...' is a regular expression (or just a word, if you prefer) that indicates what you are searching for.

var tags = [, ];

  • share this post:
  • email a friend
  • float this post
  • digg this post
  • share on stumbleupon
  • submit to technorati
  • tweet this post

end blog post

most viewed this week

least viewed this week