Monday, March 4, 2013

How do I search multiple files for a specific word in one fell swoop?

# To list files that contain "the phrase"
grep 'the phrase' -li -r /etc
# To search for "the phrase" within a specific file
grep 'the phrase' /etc/httpd/conf/httpd.conf
This command is particularily useful if you're searching a folder full of MS Word docs. Word docs aren't plain text files (duh) and therefore don't get matched properly by most application level search features (e.g. TextWrangler).

The 'l' option is shorthand for '--files-with-matches'. Only the names of files containing selected lines are written to standard output.

The 'i' option is shorthand for '--ignore-case'. Omit this option if you do want your search to be case sensitive.

Lastly, the 'r' option is shorthand for '--recursive'. This tells grep to recursively search the subdirectories listed. This is key when you're interested in searching a folder full of docs (as opposed to an individual file).

No comments:

Post a Comment

About Me

My photo
I code. I figured I should start a blog that keeps track of the many questions and answers that are asked and answered along the way. The name of my blog is "One Q, One A". The name describes the format. When searching for an answer to a problem, I typically have to visit more than one site to get enough information to solve the issue at hand. I always end up on stackoverflow.com, quora.com, random blogs, etc before the answer is obtained. In my blog, each post will consist of one question and one answer. All the noise encountered along the way will be omitted.