我想复制五个文件的内容到一个文件。我试着对每个文件使用cp。但是这会覆盖从上一个文件复制的内容。我也试过
paste -d "\n" 1.txt 0.txt
但这并没有奏效。
我希望我的脚本在每个文本文件的末尾添加换行符。
如。文件1.txt, 2.txt, 3.txt。把1,2,3的内容放在0.txt中
我该怎么做?
我想复制五个文件的内容到一个文件。我试着对每个文件使用cp。但是这会覆盖从上一个文件复制的内容。我也试过
paste -d "\n" 1.txt 0.txt
但这并没有奏效。
我希望我的脚本在每个文本文件的末尾添加换行符。
如。文件1.txt, 2.txt, 3.txt。把1,2,3的内容放在0.txt中
我该怎么做?
当前回答
如果您有一个特定的输出类型,那么执行如下操作
cat /path/to/files/*.txt >> finalout.txt
其他回答
如果你所有的文件名称都类似,你可以简单地这样做:
cat *.log >> output.log
发送多个文件到一个文件(text .txt):
cat *.txt > textall.txt
另一个选择,对于那些像我一样偶然发现这篇文章的人来说,是使用find -exec:
find . -type f -name '*.txt' -exec cat {} + >> output.file
在我的例子中,我需要一个更健壮的选项,可以查看多个子目录,所以我选择使用find。分解一下:
find .
查看当前工作目录。
-type f
只对文件感兴趣,而不是目录等。
-name '*.txt'
按名称缩减结果集
-exec cat {} +
对每个结果执行cat命令。"+"表示只生成一个cat实例(thx @gniourf_gniourf)
>> output.file
正如在其他答案中解释的那样,将cat-ed内容附加到输出文件的末尾。
如果您所有的文件都在一个目录中,您可以简单地这样做
猫* > .txt
文件1.txt,2.txt, ..会进入0.txt
If the original file contains non-printable characters, they will be lost when using the cat command. Using 'cat -v', the non-printables will be converted to visible character strings, but the output file would still not contain the actual non-printables characters in the original file. With a small number of files, an alternative might be to open the first file in an editor (e.g. vim) that handles non-printing characters. Then maneuver to the bottom of the file and enter ":r second_file_name". That will pull in the second file, including non-printing characters. The same could be done for additional files. When all files have been read in, enter ":w". The end result is that the first file will now contain what it did originally, plus the content of the files that were read in.