Using grep -rin with the string value has underscores

5,745

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' .
Share:
5,745

Related videos on Youtube

garbage collection
Author by

garbage collection

Author of Just Enough Ruby to Get By Follow me @jasoki

Updated on September 18, 2022

Comments

  • garbage collection
    garbage collection over 1 year

    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?

    • htorque
      htorque over 12 years
      Can you explain "mess up the grep command"? It works fine here in Ubuntu 11.10.