I have encountered several times the case when I want to search a part of the word, grep fails to find those matches. It works when I search full word.

For example, I need to find all files that contain "mysql_" word. Because I am dealing with legacy code that uses "mysql" instead of "mysqli" to work with a database, and want to see how much of the code uses "mysql".

Now, my command is

grep -rnw './' -e "mysql_"


grep -P -rnw '\bmysql_' .

This still doesn't work.

But if I type:

grep -rnw './' -e "mysql_query"

then of course, grep finds all matches.

What am I doing wrong?

1 Answer 1


Because you are using the '-w' parameter which means that your search pattern should occur as a full word (in other words, is delimited "non-word" characters):

  -w, --word-regexp
          Select only those lines containing matches that form  whole  words.   The
          test  is  that  the matching substring must either be at the beginning of
          the line, or preceded by a non-word constituent character.  Similarly, it
          must  be  either  at  the  end  of  the  line  or  followed by a non-word
          constituent character.  Word-constituent characters are letters,  digits,
          and the underscore.

Note that in a regex, an underscore is a "word" character..

Since mysql_ only appears as part as a word (as in mysql_query), grep -w doesn't report it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.