Example: Find all JavaScript files in the current directory and search for the word “foo”. The easiest way to do this is to execute the command
grep -iH foo `find . -name "*.js"`
Explanation:
- Assemble a list of all JavaScript files in the current directory:
find . -name "*.js" - Insert that list after “foo”: backquotes.
- Go through a list of files, search for the text “foo”:
grep -i foo <list of files>The option i ensure that the search is case-insensitive.
find . -name "*.js" -exec grep -iH foo {} \;
Explanation:
- The command after -exec is invoked for each file name that matches the pattern “*.js”, after the curly braces {} have been replaced with the name. The escaped semicolon “\;” tells -exec where the command ends.
- The option H tells grep to print the file name when it finds the text (something it doesn’t normally do if it searches a single file).
1 comment:
I found ack (http://betterthangrep.com) invaluable in exactly these situations. My use of grep declines with the same pace as my use of ack increases.
Debian users: The package is called ack-grep, unfortunately.
Post a Comment