我有两个文件:file1和file2。如何将file2的内容追加到file1,使file1的内容持久化进程?
当前回答
Cat file2 >> file1
>>操作符将输出追加到命名文件,如果命名文件不存在,则创建该文件。
Cat file1 file2 > file3
这将两个或多个文件连接到一个文件。您可以拥有任意数量的源文件。例如,
Cat *.txt >> newfile.txt
Update 20130902 In the comments eumiro suggests "don't try cat file1 file2 > file1." The reason this might not result in the expected outcome is that the file receiving the redirect is prepared before the command to the left of the > is executed. In this case, first file1 is truncated to zero length and opened for output, then the cat command attempts to concatenate the now zero-length file plus the contents of file2 into file1. The result is that the original contents of file1 are lost and in its place is a copy of file2 which probably isn't what was expected.
更新20160919 在评论中,tpartee建议链接到支持信息/来源。为了获得权威的参考,我引导善良的读者去linuxcommand.org的sh手册页,上面写着:
在执行命令之前,命令的输入和输出可能会被重定向 使用shell解释的特殊符号。
虽然这确实告诉读者他们需要知道什么,但如果你没有逐字逐句地寻找和分析语句,就很容易错过。这里最重要的单词是before。执行命令前重定向已完成(或失败)。
在cat file1 file2 > file1的示例中,shell首先执行重定向,以便I/O句柄在执行命令之前位于执行命令的环境中。
在Ian Allen的网站上可以以Linux课件的形式找到一个更友好的版本,其中详细介绍了重定向优先级。他的I/O重定向笔记页面中有很多关于这个主题的内容,包括重定向即使没有命令也能工作。把这个传递给shell:
$ >out
...创建一个名为out的空文件。shell首先设置I/O重定向,然后查找命令,没有找到命令,然后完成操作。
其他回答
另一个解决方案:
tee < file1 -a file2
Tee的好处是,你可以添加到尽可能多的文件,例如:
tee < file1 -a file2 file3 file3
将file1的内容追加到file2、file3和fil4。
从手册页:
-a, --append
append to the given FILEs, do not overwrite
Cat file2 >> file1
>>操作符将输出追加到命名文件,如果命名文件不存在,则创建该文件。
Cat file1 file2 > file3
这将两个或多个文件连接到一个文件。您可以拥有任意数量的源文件。例如,
Cat *.txt >> newfile.txt
Update 20130902 In the comments eumiro suggests "don't try cat file1 file2 > file1." The reason this might not result in the expected outcome is that the file receiving the redirect is prepared before the command to the left of the > is executed. In this case, first file1 is truncated to zero length and opened for output, then the cat command attempts to concatenate the now zero-length file plus the contents of file2 into file1. The result is that the original contents of file1 are lost and in its place is a copy of file2 which probably isn't what was expected.
更新20160919 在评论中,tpartee建议链接到支持信息/来源。为了获得权威的参考,我引导善良的读者去linuxcommand.org的sh手册页,上面写着:
在执行命令之前,命令的输入和输出可能会被重定向 使用shell解释的特殊符号。
虽然这确实告诉读者他们需要知道什么,但如果你没有逐字逐句地寻找和分析语句,就很容易错过。这里最重要的单词是before。执行命令前重定向已完成(或失败)。
在cat file1 file2 > file1的示例中,shell首先执行重定向,以便I/O句柄在执行命令之前位于执行命令的环境中。
在Ian Allen的网站上可以以Linux课件的形式找到一个更友好的版本,其中详细介绍了重定向优先级。他的I/O重定向笔记页面中有很多关于这个主题的内容,包括重定向即使没有命令也能工作。把这个传递给shell:
$ >out
...创建一个名为out的空文件。shell首先设置I/O重定向,然后查找命令,没有找到命令,然后完成操作。
仅供参考,使用ddrescue提供了一种可中断的方式来完成任务,例如,如果你有大文件,需要暂停,然后在稍后的某个点继续执行:
ddrescue -o $(wc --bytes file1 | awk '{ print $1 }') file2 file1 logfile
日志文件是重要的位。您可以使用Ctrl-C中断该进程,并通过再次指定完全相同的命令来恢复它,ddrescue将读取日志文件并从它停止的地方恢复。-o A标志告诉ddrescue从输出文件(file1)中的字节A开始。所以wc——bytes file1 | awk '{print $1}'只是以字节为单位提取file1的大小(如果你喜欢,你可以直接从ls中粘贴输出)。
正如ngks在评论中指出的,缺点是ddrescue在默认情况下可能不会被安装,所以你必须手动安装它。另一个复杂的问题是,在你的存储库中可能有两个版本的ddrescue:查看这个askubuntu问题以获得更多信息。你想要的版本是GNU的ddrescue,在基于debian的系统上是名为gddrescue的包:
sudo apt install gddrescue
对于其他发行版,请检查您的包管理系统中GNU版本的ddrescue。
Cat可以是简单的解决方案,但当我们连接大文件时变得非常缓慢,find -print可以拯救你,尽管你必须使用Cat一次。
amey@xps ~/work/python/tmp $ ls -lhtr
total 969M
-rw-r--r-- 1 amey amey 485M May 24 23:54 bigFile2.txt
-rw-r--r-- 1 amey amey 485M May 24 23:55 bigFile1.txt
amey@xps ~/work/python/tmp $ time cat bigFile1.txt bigFile2.txt >> out.txt
real 0m3.084s
user 0m0.012s
sys 0m2.308s
amey@xps ~/work/python/tmp $ time find . -maxdepth 1 -type f -name 'bigFile*' -print0 | xargs -0 cat -- > outFile1
real 0m2.516s
user 0m0.028s
sys 0m2.204s
Zsh特定:你也可以在没有cat的情况下做到这一点,尽管老实说cat更可读:
>> file1 < file2
>>将STDIN附加到file1, < dumps file2附加到STDIN。
推荐文章
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- 为什么用反斜杠开始shell命令?
- 如何找到Java堆大小和内存使用(Linux)?
- 遍历带空格的文件列表
- 在Bash中检查传递的参数是否为文件或目录
- Bash:无限睡眠(无限阻塞)
- 递归复制文件夹,不包括一些文件夹
- 如何使Python脚本在Linux中像服务或守护进程一样运行
- 如何在文件中grep不区分大小写的字符串?
- 如何使用文件的行作为命令的参数?
- Python列表目录,子目录和文件
- time_t最终的类型定义是什么?
- 我如何知道我在S3桶中存储了多少对象?
- 如何从命令输出中获得第二列?
- 如何使用cut为分隔符指定更多的空格?