简体   繁体   English

如何从 shell 在 Linux 中将 append 一个文件转换为另一个文件?

[英]How to append one file to another in Linux from the shell?

I have two files: file1 and file2 .我有两个文件: file1file2 How do I append the contents of file2 to file1 so that contents of file1 persist the process?我如何 append 将file2的内容转换为file1以便file1的内容保持该过程?

使用bash内置重定向(tldp)

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

This concatenates two or more files to one. 这将两个或多个文件连接到一个文件。 You can have as many source files as you need. 您可以根据需要拥有尽可能多的源文件。 For example, 例如,

cat *.txt >> newfile.txt

Update 20130902 更新20130902
In the comments eumiro suggests "don't try cat file1 file2 > file1 ." 在评论eumiro建议“不要尝试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 . 在这种情况下,第一个file1被截断为零长度并打开输出,然后cat命令尝试将现在的零长度文件和file2的内容连接到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. 结果是file1的原始内容丢失,并且取而代之的是file2的副本,这可能不是预期的。

Update 20160919 更新20160919
In the comments tpartee suggests linking to backing information/sources. 在评论中,tpartee建议链接到支持信息/来源。 For an authoritative reference, I direct the kind reader to the sh man page at linuxcommand.org which states: 对于权威参考,我将善意的读者引导到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) before the 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. 可以在Ian Allen的网站上以Linux课件的形式找到一个更加友好的版本,其中重定向优先级可以详细介绍。 His I/O Redirection Notes page has much to say on the topic, including the observation that redirection works even without a command. 他的I / O重定向注释页面对该主题有很多话要说,包括即使没有命令也可以进行重定向的观察。 Passing this to the shell: 把它传递给shell:

$ >out

...creates an empty file named out. ...创建一个名为out的空文件。 The shell first sets up the I/O redirection, then looks for a command, finds none, and completes the operation. shell首先设置I / O重定向,然后查找命令,找不到,并完成操作。

Note : if you need to use sudo , do this: 注意 :如果您需要使用sudo ,请执行以下操作:

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添加到命令的常用方法将失败,因为权限提升不会延续到输出重定向中。

试试这个命令:

cat file2 >> file1

你寻求的命令是

cat file2 >> file1

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 $1 }') file2 file1 logfile

The logfile is the important bit. logfile是重要的一点。 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. 您可以使用Ctrl-C中断该过程并通过再次指定完全相同的命令来恢复它,ddrescue将读取logfile并从中断处继续。 The -o A flag tells ddrescue to start from byte A in the output file ( file1 ). -o A标志告诉ddrescue从输出文件( file1 )中的字节A开始。 So wc --bytes file1 | awk '{ print $1 }' 所以wc --bytes file1 | awk '{ print $1 }' 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). wc --bytes file1 | awk '{ print $1 }'只是以字节为单位提取file1的大小(如果你愿意,你可以粘贴ls的输出)。

As pointed out by ngks in the comments, the downside is that ddrescue will probably not be installed by default, so you will have to install it manually. 正如评论中的ngks所指出的,缺点是默认情况下可能不会安装ddrescue,因此您必须手动安装它。 The other complication is that there are two versions of ddrescue which might be in your repositories: see this askubuntu question for more info. 另一个复杂因素是ddrescue有两个版本可能存在于您的存储库中:有关详细信息,请参阅此askubuntu问题 The version you want is the GNU ddrescue, and on Debian-based systems is the package named gddrescue : 你想要的版本是GNU ddrescue,在基于Debian的系统上是名为gddrescue的包:

sudo apt install gddrescue

For other distros check your package management system for the GNU version of ddrescue. 对于其他发行版,请检查您的包管理系统以获取GNU版本的ddrescue。

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的内容追加到file2file3file4

From the man page: 从手册页:

-a, --append
       append to the given FILEs, do not overwrite

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

You can also do this without cat , though honestly cat is more readable: 你也可以在没有cat情况下做到这一点,但老实说, cat更具可读性:

>> file1 < file2

The >> appends STDIN to file1 and the < dumps file2 to STDIN . >>STDIN附加到file1 ,将< dumps file2附加到STDIN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Linux中将内容从一个文件追加到另一个文件 - Append content from one file to another in Linux 如何从文件中提取特定行并将其附加到Shell脚本中的另一个现有文件中,然后从原始文件中删除? - how to extract specific lines from file and append it to another existing file in shell script and then delete from original? 如何从1个txt文件中提取值并附加到另一个文件中? - How to take value from 1 txt file and append to another one? 在 Linux Shell 中查找一个文件中的行而不是另一个文件中的行 - find lines in one file but not in another file in Linux Shell 如何突出显示另一个文件中一个文件中的单词? (Linux) - How to highlight words from one file in another file? (Linux) 过滤来自特定文件的文本和 append 和 output 到另一个文件 Linux - Filter a text from a specific file and append the output to another file Linux Linux Shell脚本复制目录中文件的最后几行以追加到另一个目录中的文件 - Linux shell script to copy last lines of a file in a directory to append to a file in another directory 如何在Linux中从一个文件搜索字符串并更新到另一个文件 - how to search a string from one file and update to another in Linux 如何将日期和时间戳从一种格式转换为另一种格式并在 Linux Shell 脚本中进行比较 - How to convert Date & Timsestamp from one format to another & Compare in Linux Shell Scripting 从另一个文件夹linux shell中删除一个文件夹中的文件 - removing files which are in one folder from another folder linux shell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM