- How to append one file to another in Linux from the shell?
- 8 Answers 8
- How to Join or Merge Text Files in Linux
- View Contents of File in Linux
- Join Contents of Three Files in Linux
- Append File Contents to New File in Linux
- Merge Contents of Files Using Sed Command
- Linux File Append Operation
- 2 Answers 2
- Merge Files in the Linux Command Line
- Use the cat command to merge files in Linux
- Append changes to the existing file
- Automate the merging files using the loop in Linux
- Use the sed command to merge files in Linux (temporary)
- Wrapping Up
How to append one file to another in Linux from the shell?
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 8
@BijayRungta: It sounds like you answered your own question. You’d pre-pend sudo to the cat command (and enter credentials if prompted).
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
@Sigur: Unless there’s a way to direct the output to two files at once, it would involve two invocations of the command.
@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.
The >> operator appends the output to the named file or creates the named file if it does not exist.
This concatenates two or more files to one. You can have as many source files as you need. For example,
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:
. 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.
How to Join or Merge Text Files in Linux
The Linux cat command is one of the most versatile tools that can use to create files, view them, and even combine them in the Linux command line.
In this article, we take a detour and explore how you can join two text files in Linux using the cat command, (short for “concatenate”) is one of the most commonly used commands in Linux as well as other UNIX-like operating systems, used to concatenate files and print on the standard output.
It is not only used to view files but can also be used to create files together with the redirection character.
View Contents of File in Linux
Suppose you have three text files: sample1.txt, sample2.txt, and sample.3.txt.
To view the contents of these files without opening them, you can use the cat command as shown (remember to replace sample1.txt , sample2.txt and domains3.txt with the names of the files you wish to combine):
$ cat sample1.txt sample2.txt sample3.txt
This provides the following output, with each line in the output corresponding to the files in order of appearance.
Join Contents of Three Files in Linux
To join the three files into one text file, we will use the output redirection operator (>) to redirect output from all the files to a new file. In this example, we have redirected content from all three files to sample4.txt.
$ cat sample1.txt sample2.txt sample3.txt >
sample4.txt
The new file now contains content from all the text files, which you can verify by running the following command.
CAUTION: The sample4.txt file is overwritten if it already exists. Therefore proceed with caution when using the redirection operator.
A better option is to append the content of the files to an already existing file. This prevents the deletion of pre-existing content. To achieve this, use the double redirection operator (>>) followed by the file name of the file you want to append the content.
The previous command can be modified as follows:
$ cat sample1.txt sample2.txt sample3.txt >>
sample4.txt
This ensures that the existing file is not overwritten. Instead content from the other files is simply added or appended to it.
Append File Contents to New File in Linux
Alternatively, to append content to the file, simply type the cat command followed by the double redirection operator and then the name of the file. Upon pressing ENTER, type in the content you want to add. Then hit ENTER again and press ctrl + d to save the changes made.
Merge Contents of Files Using Sed Command
Alternatively, you can also use the popular sed (a streamer editor) to join or merge the content of two or more files on the command-line, by using its r flag, which instructs sed to read the file provided as an argument. If there are many files, it reads all of them and displays their content as a combined output.
$ sed r sample1.txt sample1.txt sample3.txt $ sed r sample1.txt sample1.txt sample3.txt >
sample4.txt $ cat sample4.txt
That was a short guide on how you can join two or more text files on Linux. Any additional ideas you might have up your sleeve? Do let us know in the comment section.
Linux File Append Operation
when the 2nd operation is performed above, will the file be just appended without actually being erased?
The way the above operation is carried out will affect the flash wear-out.
2 Answers 2
The second operation will not erase the (first) file. But it does move the data of the second file.
Erasing a file on any *nix system means removing the inode (which is just a pointer to where on the disk/ssd/flash the file actually starts.) The actual data is not overwritten or even moved.
An example of inode data looks like this:
$ stat file.sh File: ‘file.sh’ Size: 289 Blocks: 8 IO Block: 4096 regular file Device: 813h/2067d Inode: 528981 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-07-24 13:06:57.806899456 +0200 Modify: 2015-07-24 13:06:54.822899376 +0200 Change: 2015-07-24 13:06:54.822899376 +0200 Birth: -
If it affects flash wear-out depends on the file size; if you have many very small files and lots of operations then the inodes can take up a few percent of the actual (disk/flash) size. If you have few operations (and perhaps large files) then I would not worry about flash wear out.
When the 2nd operation is performed above, will the file be just appended without actually being erased?
File/Stream opening operations are implemented in Linux using fopen() for performing the required operations. See man 3 fopen for more info.
FILE *fopen(const char *path, const char *mode);
The fopen() function opens the file whose name is the string pointed to by path and associates a stream with it.
The 2 modes for appending are :-
a —— Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
a+ —— Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
Opening a file in append mode (a as the first character of mode) causes all subsequent write operations to this stream to occur at end-of-file, as if preceded the call:
Also, the best way to open an existing file for appending and to create a file that doesn’t exist is to make two separate calls to fopen(). The first call tries to append to the file. If that fails (when fopen() returns 0), then try opening the file using the «w» mode.
Merge Files in the Linux Command Line
Learn various ways of merging multiple files into another file in the Linux command line.
Got two or more files and need to merge them in a single file? The simplest way would be to use the cat command. After all, the cat command’s original purpose is to concatenate files.
Use the cat command to merge files in Linux
Merging two files is simple. You just have to append the filename to the cat command and that’s it:
As you can see, I used the cat command to show the contents of a file and then merged them.
But it won’t save any changes. To save those changes, you have to redirect the file contents to another file.
Remember, the > will override the contents of the file so I would recommend using a new file as the cat will create the file if it doesn’t exist.
So how can you modify the editing file while keeping the previous content intact?
Append changes to the existing file
To modify the existing file, you just have to use >> instead of single > and you’d be good to go.
For example, I will make edit the existing file File_3.txt by appending Hello.txt and Sagar.txt :
As you can see, it added new text in the end line while keeping the old content intact.
Automate the merging files using the loop in Linux
Here, I will be using for loop (till three iterations as I want to merge three files) also you can use > or >> as per your needs.
for i in ; do cat "File_$i.txt" >> NewFile.txt; done
Use the sed command to merge files in Linux (temporary)
There are many times when you want to apply changes only for a specific time and in those cases, you can use sed.
Being a non-interactive way of editing files, the sed utility can be tremendously useful when used in the right way.
And when used with the h flag, it will hold the buffer temporarily:
Wrapping Up
This was my take on how you can merge files using the sed and cat command. And if you have any queries, leave us a comment.