How to concatenate files with the same prefix (and many prefixes)?

后端 未结 4 822
死守一世寂寞
死守一世寂寞 2021-01-15 23:21

I have many files that have the same prefix, only the bit after the underscore is different. And I have many prefixes as well! Underscore does not appear anywhere else in th

4条回答
  •  遥遥无期
    2021-01-16 00:01

    In case your amount of files is very large, then sometimes just using shell globbing (prefix_* and the like) isn't suitable.

    You can use a loop and append them one by one then:

    find dir -type f -name 'prefix_*' -exec bash -c 'cat "{}" >> result' \;
    

    This will append all files matching prefix_* one by one to the file result (which shouldn't exist in the beginning, if in doubt use rm result).

    If you have lots of different prefixes, you can of course append one group after the other without removing the result file in between.

    All the other options the Unix tool find offers can be used as well of course. But if you need help with that, feel free to ask again.

提交回复
热议问题