How to cat multiple files from a list of files in Bash?

后端 未结 6 891
故里飘歌
故里飘歌 2020-12-16 18:08

I have a text file that holds a list of files. I want to cat their contents together. What is the best way to do this? I was doing something like this but it

6条回答
  •  醉酒成梦
    2020-12-16 18:45

    xargs

    The advantage of xargs over $(cat) is that cat expands to a huge list of arguments which could fail if you have a lot of files in the list:

    printf 'a\nb\n#c\n' > files
    printf '12\n3\n' > a
    printf '4\n56\n' > b
    printf  '8\n9\n' > c
    # Optional grep to remove lines starting with #
    # as requested by the OP.
    grep -v '^#' files | xargs cat
    

    Output:

    12
    3
    4
    56
    

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

提交回复
热议问题