I would like to search recursively in all the files and directories under a directory for some strings e.g. library.

I also would like to exclude files and directories which store downloaded webpages but are not .html or .htm files (which are still of my interest), such as:

  • files whose filenames have certain extensions, such as .js, .css, .png, .gif,.jpeg, and
  • directories whose names are ended with _files.

so how shall I do that? I am thinking about using locate before find, because locate is faster, but I don't know locate can do it or not. Thanks.

1 Answer 1


Something like this might do what you want:

find . -name '*_files' -type d -prune -o ! \( -name '*.js' -o -name '*.css' -o -name '*.png' -o -name '*.gif' -o -name '*.jpeg' \)

If that prints out a reasonable list then add -print0 | xargs -o grep -i library to do the actual searching.

  • Thanks! Will your way be faster than first locate for library, and then do some text processing (?) on its output to eliminate those unwanted files and dirs?
    – Tim
    Aug 15, 2014 at 3:26
  • locate searches for file names not contents. So you can't locate library and have that do anything even close to what you want. That being said, if you could use locate as a first pass for this operation that might be faster than just using find directly but that is going to depend on the volume of files find has to search and other factors. Aug 15, 2014 at 3:34
  • doesn't find also search among file names not contents?
    – Tim
    Aug 15, 2014 at 3:39
  • @Tim Yes, which is why the output is sent to grep to search for library in the contents. You could use locate for the file name matching (but only in the positive direction I believe). Aug 15, 2014 at 3:41
  • What do you mean by "in the positive direction"?
    – Tim
    Aug 15, 2014 at 3:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .