How do you grep results from 'find'?

前端 未结 3 2119
情深已故
情深已故 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:27

    When too many files exist for the * expansion to run:

    $ grep -o 'xxmaj\|xxbos\|xxfld' train/* | wc -l
    -bash: /bin/grep: Argument list too long
    0
    

    Then this code fixes the “too long” problem:

    $ find junk -maxdepth 1 -type f | xargs grep -o 'TVDetails\|xxmaj\|xxbos\|xxfld' 
    junk/gum-.doc.out:TVDetails
    junk/Zv0n.doc.out:TVDetails
    $ find junk -maxdepth 1 -type f | xargs grep -o 'TVDetails\|xxmaj\|xxbos\|xxfld' | wc -l
    2
    

    It runs faster on my system, and maybe yours, when using the -P 0 option:

    $ /usr/bin/time -f "%E Elapsed Real Time" find train -maxdepth 1 -type f | xargs -P 0 grep -o 'TVDetails\|xxmaj\|xxbos\|xxfld' | wc -l
    0:02.45 Elapsed Real Time
    358
    $ /usr/bin/time -f "%E Elapsed Real Time" find train -maxdepth 1 -type f | xargs grep -o 'TVDetails\|xxmaj\|xxbos\|xxfld' | wc -l
    0:11.96 Elapsed Real Time
    358
    

    Hope this helps.

提交回复
热议问题