将目录中所有*.txt文件合并为一个大文本文件的最快和最实用的方法是什么?

目前我正在使用cygwin的窗口,所以我可以访问BASH。

Windows shell命令也会很好,但我怀疑有没有一个。


当前回答

这种方法怎么样?

find . -type f -name '*.txt' -exec cat {} + >> output.txt

其他回答

当你遇到问题,把all.txt转换成all.txt, 您可以尝试检查all.txt是否存在,如果存在,删除

是这样的:

[-e $"all.txt"] && rm $"all.txt"

这种方法怎么样?

find . -type f -name '*.txt' -exec cat {} + >> output.txt

要小心,因为这些方法都不能处理大量文件。就我个人而言,我用了这句话:

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

编辑:就像有人在评论中说的,你可以用$(ls *.txt)替换$(ls | grep ".txt")

编辑:感谢@gnourf_gnourf的专业知识,使用glob是在目录中迭代文件的正确方法。因此,像$(ls | grep ".txt")这样亵渎神灵的表达式必须被*.txt取代(请参阅此处的文章)。

好的解决方案

for i in *.txt;do cat $i >> output.txt;done
type [source folder]\*.[File extension] > [destination folder]\[file name].[File extension]

例如:

type C:\*.txt > C:\1\all.txt

这将采取所有的txt文件在C:\文件夹,并将其保存在C:\1文件夹的名称为all.txt

Or

type [source folder]\* > [destination folder]\[file name].[File extension]

例如:

type C:\* > C:\1\all.txt

这将获取文件夹中所有文件,并将Content放在C:\1\all.txt中

所有这些都令人讨厌....

ls | grep *.txt | while read file; do cat $file >> ./output.txt; done;

简单的东西。