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?
8 Answers
Use bash builtin redirection (tldp):
cat file2 >> file1
-
3How do you do it destination file is not owned by you and you need to use sudo? Feb 6, 2014 at 19:30
-
1@BijayRungta: It sounds like you answered your own question. You'd pre-pend
sudo
to thecat
command (and enter credentials if prompted).– DavidFeb 6, 2014 at 19:46 -
you need to ... chmod 777 /etc/default/docker to give yourself write permissions on that file - be sure to restore the old file permissions once done– danday74Mar 9, 2016 at 18:54
-
1@Sigur: Unless there’s a way to direct the output to two files at once, it would involve two invocations of the command.– DavidSep 30, 2017 at 17:45
-
3@Sigur or have a look at the
tee
program:cat 1 | tee -a 2 3
. You can put as many files as you like after the--append
(or-a
for short) switch. Oct 7, 2017 at 15:21
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
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.
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 page at linuxcommand.org which states:
Before a command is executed, its input and output may be redirected using a special notation interpreted by the 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.
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 Notes page has much to say on the topic, including the observation that redirection works even without a command. Passing this to the shell:
$ >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.
-
18@asir - don't try
cat file1 file2 > file1
- this won't work like you are probably awaiting.– eumiroFeb 11, 2011 at 13:43 -
5Actually this is exactly what he needs. He says "without overwriting the current file1." The first three answerers have completely ignored this part of the question and suggested a command using
>>
which will modify the filefile1
. T.Rob did a far superior job of explaining his answer rather than just racing to submit something that was, in point of fact, incorrect. Based on the text of the question, I believe thatcat file1 file2 > file3
is the appropriate command that @asir was looking for.– dm78Sep 2, 2013 at 15:44 -
Thanks for the kind words, David! What @eumiro points out above but doesn't go into detail on is that the operation to the right of the
>
is executed first. So executingcat file1 file2 > file1
would first clobberfile1
then attempt to copy the now-zero-length file onto itself. This makes sense when you think about the order in which the operations could and should occur but is subtle enough that it catches many people by surprise. So if nothing else, eumiro and you have prompted a further improvement to the answer. Thanks for that!– T.RobSep 2, 2013 at 18:57 -
Also don't try cat file1 >> file1, this will cause the file to be recursively rewritten, I mistakenly do this and just within a few seconds, 50 million lines have been put in to the file from just previously a few dozen lines.– O.OMar 20, 2017 at 6:56
-
Also, just to make it a bit more concise, if the "new file" already exists,
>>
appends to the file and>
replaces the file.– rinogoSep 3, 2018 at 22:19
Note: if you need to use sudo, do this:
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.
-
7Another common idiom for this is
cat file2 | sudo tee -a file1 > /dev/null
– ianwJan 20, 2016 at 20:29
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 -o $(wc --bytes file1 | awk '{ print $1 }') 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 A in 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).
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. The other complication is that there are two versions of ddrescue which might be in your repositories: see this askubuntu question for more info. The version you want is the GNU ddrescue, and on Debian-based systems is the package named gddrescue
:
sudo apt install gddrescue
For other distros check your package management system for the GNU version of ddrescue.
-
1For the benefit of new users: ddrescue is a GNU tool, but it may not exist on your Linux, Mac or other unix-like system. ddrescue doesn't appear to be required by POSIX or any other standard.– user2065875Aug 19, 2014 at 18:49
Another solution:
tee < file1 -a file2
tee
has the benefit that you can append to as many files as you like, for example:
tee < file1 -a file2 file3 file3
will append the contents of file1
to file2
, file3
and file4
.
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.
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
-
The time savings you report for your find/cat combo command is because you are timing the find command only which is printing out the names of the files. Try timing the entire command like this:
time (find . -maxdepth 1 -type f -name 'bigFile*' -print0 | xargs -0 cat -- > outFile1)
and it should product similar results to your cat only command.– JoshMcMar 14, 2017 at 5:29
You can also do this without cat
, though honestly cat
is more readable:
>> file1 < file2
The >>
appends STDIN to file1
and the <
dumps file2
to STDIN.
-
5
-
1@user202729 You're right, it doesn't work in bash. It does work in zsh. May 29, 2018 at 5:08