How to pipe list of files returned by find command to cat to view all the files

后端 未结 15 1319
庸人自扰
庸人自扰 2020-12-02 03:44

I am doing a find and then getting a list of files. How do I pipe it to another utility like cat (so that cat displays the contents of all those fi

相关标签:
15条回答
  • 2020-12-02 03:57

    To list and see contents of all abc.def files on a server in the directories /ghi and /jkl

    find /ghi /jkl -type f -name abc.def 2> /dev/null -exec ls {} \; -exec cat {} \;
    

    To list the abc.def files which have commented entries and display see those entries in the directories /ghi and /jkl

    find /ghi /jkl -type f -name abc.def 2> /dev/null -exec grep -H ^# {} \;
    
    0 讨论(0)
  • 2020-12-02 03:58

    Sounds like a job for a shell script to me:

    for file in 'find -name *.xml'
    do
       grep 'hello' file
    done
    

    or something like that

    0 讨论(0)
  • 2020-12-02 04:02

    To achieve this (using bash) I would do as follows:

    cat $(find . -name '*.foo')
    

    This is known as the "command substitution" and it strips line feed by default which is really convinient !

    more infos here

    0 讨论(0)
  • 2020-12-02 04:04

    Here is my shot for general use:

    grep YOURSTRING `find .`
    

    It will print the file name

    0 讨论(0)
  • 2020-12-02 04:05

    In bash, the following would be appropriate:

    find /dir -type f -print0 | xargs -0i cat {} | grep whatever
    

    This will find all files in the /dir directory, and safely pipe the filenames into xargs, which will safely drive grep.

    Skipping xargs is not a good idea if you have many thousands of files in /dir; cat will break due to excessive argument list length. xargs will sort that all out for you.

    The -print0 argument to find meshes with the -0 argument to xargs to handle filenames with spaces properly. The -i argument to xargs allows you to insert the filename where required in the cat command line. The brackets are replaced by the filename piped into the cat command from find.

    0 讨论(0)
  • 2020-12-02 04:07

    I use something like this:

    find . -name <filename> -print0 | xargs -0 cat | grep <word2search4>
    

    "-print0" argument for "find" and "-0" argument for "xargs" are needed to handle whitespace in file paths/names correctly.

    0 讨论(0)
提交回复
热议问题