Concatenating multiple text files into a single file in Bash

前端 未结 12 1600
眼角桃花
眼角桃花 2020-11-29 15:31

What is the quickest and most pragmatic way to combine all *.txt file in a directory into one large text file?

Currently I\'m using windows with cygwin so I have acc

12条回答
  •  Happy的楠姐
    2020-11-29 16:04

    Be careful, because none of these methods work with a large number of files. Personally, I used this line:

    for i in $(ls | grep ".txt");do cat $i >> output.txt;done
    

    EDIT: As someone said in the comments, you can replace $(ls | grep ".txt") with $(ls *.txt)

    EDIT: thanks to @gnourf_gnourf expertise, the use of glob is the correct way to iterate over files in a directory. Consequently, blasphemous expressions like $(ls | grep ".txt") must be replaced by *.txt (see the article here).

    Good Solution

    for i in *.txt;do cat $i >> output.txt;done
    

提交回复
热议问题