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

后端 未结 15 1320
庸人自扰
庸人自扰 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 04:15

    This works for me

    find _CACHE_* | while read line; do
        cat "$line" | grep "something"
    done
    
    0 讨论(0)
  • 2020-12-02 04:17
    1. Piping to another process (Although this WON'T accomplish what you said you are trying to do):

      command1 | command2
      

      This will send the output of command1 as the input of command2

    2. -exec on a find (this will do what you are wanting to do -- but is specific to find)

      find . -name '*.foo' -exec cat {} \;
      

      (Everything between find and -exec are the find predicates you were already using. {} will substitute the particular file you found into the command (cat {} in this case); the \; is to end the -exec command.)

    3. send output of one process as command line arguments to another process

      command2 `command1`
      

      for example:

      cat `find . -name '*.foo' -print`
      

      (Note these are BACK-QUOTES not regular quotes (under the tilde ~ on my keyboard).) This will send the output of command1 into command2 as command line arguments. Note that file names containing spaces (newlines, etc) will be broken into separate arguments, though.

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

    Here's my way to find file names that contain some content that I'm interested in, just a single bash line that nicely handles spaces in filenames too:

    find . -name \*.xml | while read i; do grep '<?xml' "$i" >/dev/null; [ $? == 0 ] && echo $i; done
    
    0 讨论(0)
提交回复
热议问题