如何从 shell 在 Linux 中将一个文件附加到另一个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4969641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append one file to another in Linux from the shell?
提问by asir
I have two files: file1
and file2
. How do I append the contents of file2
to file1
so that contents of file1
persist the process?
我有两个文件:file1
和file2
. 我如何附加file2
to的内容以file1
使file1
该过程的内容持久化?
回答by David
回答by eumiro
Try this command:
试试这个命令:
cat file2 >> file1
回答by T.Rob
cat file2 >> file1
cat file2 >> file1
The >>
operator appends the output to the named file or creates the named file if it does not exist.
该>>
运营商将输出追加到指定的文件或创建命名的文件,如果它不存在。
cat file1 file2 > file3
cat file1 file2 > file3
This concatenates two or more files to one. You can have as many source files as you need. For example,
这将两个或多个文件连接为一个。您可以根据需要拥有任意数量的源文件。例如,
cat *.txt >> newfile.txt
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.
更新 20130902
在评论中 eumiro 建议“不要尝试” cat file1 file2 > file1
。这可能不会导致预期结果的原因是接收重定向的文件是在>
执行左侧的命令之前准备好的。在这种情况下,首先file1
被截断为零长度并打开输出,然后cat
命令尝试连接现在为零长度的文件加上file2
into的内容file1
。结果是 的原始内容file1
丢失了,取而代之的是file2
可能不是预期的副本。
Update 20160919
In the comments tpartee suggests linking to backing information/sources. For an authoritative reference, I direct the kind reader to the sh man pageat linuxcommand.org which states:
更新 20160919
在评论中 tpartee 建议链接到支持信息/来源。为了获得权威参考,我将好心的读者引导至linuxcommand.org上的sh 手册页,其中指出:
Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell.
在执行命令之前,可以使用由 shell 解释的特殊符号重定向其输入和输出。
While that does tell the reader what they need to know it is easy to miss if you aren't looking for it and parsing the statement word by word. The most important word here being 'before'. The redirection is completed (or fails) beforethe command is executed.
虽然这确实告诉读者他们需要知道什么,但如果您不寻找它并逐字解析语句,则很容易错过。这里最重要的词是“之前”。重定向在命令执行之前完成(或失败)。
In the example case of cat file1 file2 > file1
the shell performs the redirection first so that the I/O handles are in place in the environment in which the command will be executed before it is executed.
在cat file1 file2 > file1
shell的示例情况下,首先执行重定向,以便 I/O 句柄在执行命令之前在命令执行的环境中就位。
A friendlier version in which the redirection precedence is covered at length can be found at Ian Allen's web site in the form of Linux courseware. His I/O Redirection Notespage has much to say on the topic, including the observation that redirection works even without a command. Passing this to the shell:
可以在 Ian Allen 的网站上以 Linux 课件的形式找到一个更友好的版本,其中详细介绍了重定向优先级。他的I/O Redirection Notes页面对这个主题有很多话要说,包括观察到重定向即使没有命令也能工作。将此传递给外壳:
$ >out
...creates an empty file named out. The shell first sets up the I/O redirection, then looks for a command, finds none, and completes the operation.
...创建一个名为 out 的空文件。shell首先设置I/O重定向,然后查找命令,没有找到,完成操作。
回答by Zorawar
Just for reference, using ddrescue provides an interruptible way of achieving the task if, for example, you have large files and the need to pause and then carry on at some later point:
仅供参考,使用 ddrescue 提供了一种可中断的方式来完成任务,例如,如果您有大文件并且需要暂停然后在稍后继续执行:
ddrescue -o $(wc --bytes file1 | awk '{ print }') file2 file1 logfile
The logfile
is the important bit. You can interrupt the process with Ctrl-C
and resume it by specifying the exact same command again and ddrescue will read logfile
and resume from where it left off. The -o A
flag tells ddrescue to start from byte Ain the output file (file1
). So wc --bytes file1 | awk '{ print $1 }'
just extracts the size of file1
in bytes (you can just paste in the output from ls
if you like).
这logfile
是重要的一点。您可以Ctrl-C
通过再次指定完全相同的命令来中断进程并恢复它,ddrescue 将从logfile
它停止的地方读取和恢复。该-o A
标志告诉 ddrescue 从输出文件 ( )中的字节A开始file1
。所以wc --bytes file1 | awk '{ print $1 }'
只需提取file1
以字节为单位的大小(ls
如果你愿意,你可以粘贴输出)。
As pointed out by ngksin the comments, the downside is that ddrescue will probably not be installed by default, so you will have to install it manually. The other complication is that there are two versions of ddrescue which might be in your repositories: see this askubuntu questionfor more info. The version you want is the GNU ddrescue, and on Debian-based systems is the package named gddrescue
:
正如ngks在评论中指出的那样,缺点是默认情况下可能不会安装 ddrescue,因此您必须手动安装它。另一个复杂问题是您的存储库中可能有两个版本的 ddrescue:有关更多信息,请参阅此 askubuntu 问题。你想要的版本是 GNU ddrescue,在基于 Debian 的系统上是名为 的包gddrescue
:
sudo apt install gddrescue
For other distros check your package management system for the GNUversion of ddrescue.
对于其他发行版,请检查您的包管理系统以获取ddrescue的GNU版本。
回答by jdunk
Note: if you need to use sudo, do this:
注意:如果您需要使用sudo,请执行以下操作:
sudo bash -c 'cat file2 >> file1'
sudo bash -c 'cat file2 >> file1'
The usual method of simply prepending sudo
to the command will fail, since the privilege escalation doesn't carry over into the output redirection.
简单地添加sudo
到命令前面的常用方法将失败,因为权限提升不会延续到输出重定向中。
回答by Amey Jadiye
cat
can be the easy solution but that become very slow when we concat large files, find -print
is to rescue you, though you have to use cat once.
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
回答by Stefan van den Akker
Another solution:
另一种解决方案:
cat file1 | tee -a file2
tee
has the benefit that you can append to as many files as you like, for example:
tee
具有可以附加到任意数量的文件的好处,例如:
cat file1 | tee -a file2 file3 file3
will append the contents of file1
to file2
, file3
and file4
.
将附加file1
到file2
,file3
和的内容file4
。
From the man page:
从手册页:
-a, --append
append to the given FILEs, do not overwrite
回答by Alok Singh
You can also do this without cat
, though honestly cat
is more readable:
您也可以在没有 的情况下执行此操作cat
,但老实说cat
更具可读性:
>> file1 < file2
>> file1 < file2
The >>
appends STDINto file1
and the <
dumps file2
to STDIN.
该>>
附加标准输入到file1
和<
转储file2
到STDIN。