我有两个文件:file1和file2。如何将file2的内容追加到file1,使file1的内容持久化进程?
当前回答
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。
注意:如果你需要使用sudo,请这样做:
Sudo bash -c 'cat file2 >> file1'
简单地将sudo放在命令前的通常方法将失败,因为特权升级不会延续到输出重定向中。
另一个解决方案:
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可以是简单的解决方案,但当我们连接大文件时变得非常缓慢,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