You are on page 1of 5

18-11-2020 how to merge multiple files into one single file in linux - lost saloon

technology

linux
related posts
how to merge how to navigate

This post and this


multiple files into one directories in
linux
website contains a liate single file in linux how to merge
links. See my disclosure two or more
about a liate links. posted on june 15, 2013 by barkeep
directories in
linux
how to enable
and disable
ipv6 in linux
how to change
Many a times you may have multiple les that
encoding in
needs to merged into one single le. It could be
gedit text editor
that you previously split a single  le into
multiple les, and want to just merge them
back or you have several log les that you want
merged into one. Whatever the reason, it is very
easy to merge multiple text les into a single
le in Linux.

The command in Linux to concatenate or


tags
merge multiple les into one le is called cat.
The cat command by default will concatenate adsense
and print out multiple les to the standard
adwords
output. You can redirect the standard output to
a le using the ‘>‘ operator to save the output to analytics bash
disk or le system. brandy business cards
chrome cli
Another useful utility to merge les is called
join that can join lines of two les based on cocktail css
https://www.lostsaloon.com/technology/how-to-merge-multiple-files-into-one-file-in-linux/ 1/5
18-11-2020 how to merge multiple files into one single file in linux - lost saloon

common elds. It can however work only on editor refox ash drive
two les at a time, and I have found it to be
quite cumbersome to use. We will cover mostly gin gnome
the cat command in this post.
google html
Merge Multiple les into One in Order internet javascript
The cat command takes a list of le names as kahlua kde
its argument. The order in which the le names
are speci ed in the command line dictates the linux
order in which the les are merged or
marketing
combined. So, if you have several les named
le1.txt, le2.txt, le3.txt etc… martini
network plasma
bash$ cat le1.txt le2.txt le3.txt le4.txt >
programming
./merged le.txt
router rss rsync rum

The above command will append the contents screenshot search


of le2.txt to the end of le1.txt. The content of
le3.txt is appended to the end of merged
seo shell small
contents of le1.txt and le2.txt and so on…and business sweet
the entire merged le is saved with the name vermouth tequila usb
merged le.txt in the current working directory.
vim vodka web
browser website
whiskey
wordpress

Many a time, you might have an inordinately


large number of les which makes it harder to
https://www.lostsaloon.com/technology/how-to-merge-multiple-files-into-one-file-in-linux/ 2/5
18-11-2020 how to merge multiple files into one single file in linux - lost saloon

type in all the le names. The cat command


accepts regular expressions as input le recent posts
names, which means you can use them to
how to add a
reduce the number of arguments.
line break in
html
bash$ cat le*.txt my*.txt > merged le.txt how to navigate
directories in
This will merge all the les in the current linux
directory that start with the name le and has a how to merge
txt extension followed by the les that start two or more
with my and has a txt extension. You have to be directories in
careful about using regular expressions, if you linux
want to preserve the order of les. If you get how to enable
the regular expression wrong, it will affect the and disable
exact order in which the les are merged. ipv6 in linux
how to change
A quick and easy way to make sure the les get encoding in
merged in the exact order you want, is to use
gedit text editor
the output of another le listing program such
as ls or nd and pipe it to the cat command.
First execute the nd command with the
regular expression and verify the le order…

bash$ nd . -name " le*.txt" -o -name


"my*.txt"

This will print the les in order such that you


can verify it to be correct or modify it to
match what you want. You can then pipe that
output into the cat command.

bash$ nd . -name " le*.txt" -o -name


"my*.txt" | xargs cat > ./merged le.txt

When you merge multiple les into one le


using regular expressions to match them,
especially when it is piped and where the
output le is not very obvious, make sure that
the regular expression does not match the
lename of the merged le. In the case that it
does match, usually the cat command is pretty
good at error-ing out with the message “input

https://www.lostsaloon.com/technology/how-to-merge-multiple-files-into-one-file-in-linux/ 3/5
18-11-2020 how to merge multiple files into one single file in linux - lost saloon

le is output le”. But it helps to be careful to


start with.

Merge Two Files at Arbitrary Location


Sometimes you might want to merge two les,
but at a particular location within the content of
a le. This is more like the process of inserting
contents of one le into an another at a
particular position in the le.

If the le sizes are small and manageable, then


vi is a great editor tool to do this. Otherwise the
option is to split the le rst and then merge
the resulting les in order. The easiest way is to
split the le is based on the line numbers,
exactly at where you want to insert the other
le.

bash$ split -l 1234 le1.txt

You can split the le into any number of output


les depending on your requirement. The above
example will split the le le1.txt to chunks of
1234 lines. It is quite possible that you might
end up with more than two les, named xaa,
xab, xac etc..You can merge all of it back using
the same cat command as mentioned earlier.

bash$ cat xaa le2.txt xa{b..z}

The above command will merge the les in


order with the contents of le2.txt in between
the contents of xaa and xab.

Another use case is when you need to merge


only speci c parts of certain les depending on
some condition. This is especially useful for
me when I have to analyze several large log
https://www.lostsaloon.com/technology/how-to-merge-multiple-files-into-one-file-in-linux/ 4/5
18-11-2020 how to merge multiple files into one single file in linux - lost saloon

les, but am only interested in certain


messages or lines. So, I will need to extract the
important log messages based on some
criteria from several log les and save them in
a different le while also maintaining or
preserving the order of the messages.

Though you can do this using cat and grep


commands, you can do it with just the grep
command as well.

bash$ grep -h "[Error]" log le*.log >


onlyerrors.log

The above will extract all the lines that match


the pattern [Error] and save it to another le.
You will have to make sure that the log les are
in order when using the regular expression to
match them, as mentioned earlier in the post.

this entry was posted in technology and tagged


linux. bookmark the permalink.

https://www.lostsaloon.com/technology/how-to-merge-multiple-files-into-one-file-in-linux/ 5/5

You might also like