MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

Find a string in files

with 2 comments

From time to time, folks ask questions about how to solve common problems in Linux or Unix. Today, the question is: “How do I find a list of files that contain a specific string?” There are two alternatives with the find command, and the following sample searches look for files that contain a sqlite3 string literal.

  • Search for only the file names:
find . -type f | xargs grep -li sqlite3

Or, the more verbose:

find . -type f -exec grep -li sqlite3 /dev/null {} +
  • Search for the file names and text line:
find . -type f | xargs grep -i sqlite3

Or, the more verbose:

find . -type f -exec grep -i sqlite3 /dev/null {} +

Don’t exclude the /dev/null from the verbose syntax or you’ll get the things you lack permissions to inspect or that raise other errors. I don’t post a lot of Linux or Unix tips and techniques, and you may find this site more useful to answer these types of questions:

Unix & Linux Stack Exchange web site

As always, I hope this helps those you land on the blog page.

Written by maclochlainn

April 18th, 2015 at 2:39 pm