Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
2  
Can you explain "mess up the grep command"? It works fine here in Ubuntu 11.10. –  htorque Nov 1 '11 at 21:10
add comment

1 Answer

up vote 1 down vote accepted

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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