User Tools

Site Tools


grep_on_find_results

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
grep_on_find_results [2021/04/08 10:01] rajugrep_on_find_results [2021/04/08 10:30] (current) raju
Line 1: Line 1:
 To grep on the results of a find command, you can use something like To grep on the results of a find command, you can use something like
 <code> <code>
-find . -iname '*writing*' -exec grep network -inH --color {} ++find . -iname '*.py' -exec grep something -inH --color {} + 
 +</code> 
 + 
 +Another solution is to use xargs 
 +<code> 
 +find . -iname '*.py' -print0 | xargs -0 grep something -inH --color 
 +</code> 
 + 
 +The output can be redirected to a file 
 +<code> 
 +find . -iname '*.py' -exec grep something -inH --color {} + > output.txt 
 +</code> 
 + 
 +Notes: 
 + 
 +Here is an excerpt from the man page of find (https://man7.org/linux/man-pages/man1/find.1.html -> search for "-exec command {} +") 
 + 
 +<code> 
 +       -exec command {} + 
 +              This variant of the -exec action runs the specified 
 +              command on the selected files, but the command line is 
 +              built by appending each selected file name at the end; the 
 +              total number of invocations of the command will be much 
 +              less than the number of matched files.  The command line 
 +              is built in much the same way that xargs builds its 
 +              command lines.  Only one instance of `{}' is allowed 
 +              within the command, and it must appear at the end, 
 +              immediately before the `+'; it needs to be escaped (with a 
 +              `\') or quoted to protect it from interpretation by the 
 +              shell.  The command is executed in the starting directory. 
 +              If any invocation with the `+' form returns a non-zero 
 +              value as exit status, then find returns a non-zero exit 
 +              status.  If find encounters an error, this can sometimes 
 +              cause an immediate exit, so some pending commands may not 
 +              be run at all.  For this reason -exec my- 
 +              command ... {} + -quit may not result in my-command 
 +              actually being run.  This variant of -exec always returns 
 +              true.
 </code> </code>
  
grep_on_find_results.1617876062.txt.gz · Last modified: 2021/04/08 10:01 by raju