How do you grep results from 'find'?

前端 未结 3 2145
情深已故
情深已故 2021-01-05 18:02

Trying to find a word/pattern contained within the resulting file names of the find command.

For instance, I have this command:

find . -name Gruntfile

3条回答
  •  情歌与酒
    2021-01-05 18:25

    Use the -exec {} + option to pass the list of filenames that are found as arguments to grep:

    find -name Gruntfile.js -exec grep -nw 'purifycss' {} +
    

    This is the safest and most efficient approach, as it doesn't break when the path to the file isn't "well-behaved" (e.g. contains a space). Like an approach using xargs, it also minimises the number of calls to grep by passing multiple filenames at once.

    I have removed the -e and -r switches, as I don't think that they're useful to you here.


    An excerpt from man find:

    -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.

提交回复
热议问题