2

I am trying to run this command.

grep -rin "wp_get_attachment_url" *

But the underscores (_) in the string value seems to mess up the grep command. How do I search the string values that have underscores?

1
  • 2
    Can you explain "mess up the grep command"? It works fine here in Ubuntu 11.10.
    – htorque
    Nov 1, 2011 at 21:10

1 Answer 1

2

If wp_get_attachment_url is actually an URL that must be interpreted literally, use single quotes to prevent the shell from interpreting characters. Furthermore, use the -F option of grep to avoid it from interpreting the pattern as a regular expression.

http://example.com/?id=1 interpreted as a regular expression would match either http://example.comid=1 or http://example.com/id=1, but not http://example.com/?id=1.

The command becomes:

grep -rinF 'http://example.com/?id=1' *

If the pattern contains a ', use '\'' to escape it:

grep -rinF 'mornin'\'' all' *

A hint: * matches all non-hidden files in the current directory. If you want to match dot-files as well, just grep the current directory recursively (-r):

grep -rinF 'search term' .

You must log in to answer this question.

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