Difference between revisions of "Grep"
Jump to navigation
Jump to search
(Created page with "==grep with find== Given the common <code>grep</code> command <syntaxhighlight lang=bash> grep -R 'pattern' * </syntaxhighlight> a corresponding <code>find -exec</code> com...") |
|||
| Line 10: | Line 10: | ||
<syntaxhighlight lang=bash> | <syntaxhighlight lang=bash> | ||
| − | find … -exec grep - | + | find … -exec grep -HR 'pattern' {} \; |
| + | </syntaxhighlight> | ||
| + | |||
| + | If necessary, use the command from [[Find#Sorting_by_mtime]] to search on files in date order: | ||
| + | |||
| + | <syntaxhighlight lang=bash> | ||
| + | find … -printf '%T@ %p\0' | | ||
| + | sort -zk 1n | # 1n for oldest first, 1nr for newest first | ||
| + | sed -z 's/^[^ ]* //' | | ||
| + | xargs -0i grep -HR 'pattern' {} | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 12:14, 9 January 2017
grep with find
Given the common grep command
grep -R 'pattern' *
a corresponding find -exec command should add -H to include the filename prefix in the results. (This is needed because the filenames are given one at a time.)
find … -exec grep -HR 'pattern' {} \;
If necessary, use the command from Find#Sorting_by_mtime to search on files in date order:
find … -printf '%T@ %p\0' |
sort -zk 1n | # 1n for oldest first, 1nr for newest first
sed -z 's/^[^ ]* //' |
xargs -0i grep -HR 'pattern' {}