2016-02-16

find xargs grep 的完美结合

例子:在当前目录中搜索.cpp和.h文件中包含关键字 'hg.dat'的文件,并打印相应的文件名行号

find ./ \( -iname "*.cpp" -o -iname "*.h" \) -print0 | xargs -0 grep -RHn --color=auto "hg.dat"

-print True; print the full file name on the standard output, followed by a newline.   If you are  piping  the  output  of
              find  into  another  program and there is the faintest possibility(极小的可能性) that the files which you are searching for might
              contain a newline, then you should seriously(认真地、严肃地) consider using the -print0 option instead of -print.  See the  UNUSUAL
              FILENAMES section for information about how unusual characters in filenames are handled.
-print0
           True; print the full file name on the standard output, followed by a null character (instead of the newline charac-
              ter that -print uses).  This allows file names that contain newlines or other types of white space to be  correctly
              interpreted by programs that process the find output.  This option corresponds to the -0 option of xargs.

xargs

 --null
       -0     Input items are terminated by a null character instead of by whitespace, and the quotes(引号) and backslash are not  spe-
              cial  (every character is taken literally).  Disables the end of file string, which is treated like any other argu-
              ment.  Useful when input items might contain white space, quote marks, or backslashes.  The GNU find -print0 option
              produces input suitable for this mode.

你可能感兴趣的:(2016-02-16)