如何连接来自不同子目录的文件?

swa*_*erj 3 find cat merge

我有一个包含许多子目录的大文件夹,每个子目录都包含许多.txt文件。我想将所有这些文件连接成一个.txt文件。我可以使用 为每个子目录执行此操作cat *.txt>merged.txt,但我正在尝试为大文件夹中的所有文件执行此操作。我该怎么做呢?

αғs*_*нιη 6

尝试

find /path/to/source -type f -name '*.txt' -exec cat {} + >mergedfile
Run Code Online (Sandbox Code Playgroud)

f/path/to/source子目录中递归查找所有 '*.txt'文件并将所有文件连接成一个mergedfile.

要连接其目录中的每个子目录文件,请执行以下操作:

find . -mindepth 1 -type d -execdir sh -c 'cat $1/*.txt >> $1/mergedfile' _ {} \;
Run Code Online (Sandbox Code Playgroud)