Being able to combine various files together into one can be very helpful. You can arrange and sort out your desktop and combine several related documents into one. In Linux, there are several ways to concatenate files together. This tutorial shows all the ways to do it: they’re all pretty simple, really.
Concatenating Files in Linux Through the Command Line
The command line is the easiest way to concatenate files in Linux. All you need is the Linux terminal or a decent terminal emulator.
There are a few terminal commands that allow you to merge files in Linux:
The most popular among them is the
cat command. You can combine multiple files together into one by using the following format:
cat "file1.txt" "file2.txt" >> "file3.txt"
It can be done with other commands as well:
join file1.txt file2.txt > file3.txt
Note: your Linux system may ask for superuser privileges to use the
join command. Add
sudo before entering the
join command to allow that.
paste -d "" file1.txt file2.txt > file3.txt
sed -e "r file2.txt" file1.txt > file3.txt
Concatenating Folders of Files
If you have a ton of files to concatenate, you can store them in folders and concatenate the contents of each folder into a single file. The only exception is the
join command, which doesn’t allow you to do that.
These commands will allow you to concatenate folders of files:
cat dog_folder/* > output_file.txt
paste -d '' dog_folder/* > output_file.txt
sed -s '' dog_folder/* > output_file.txt
These will concatenate all the files within the “dog_folder” into one file named “output_file.txt”. Do note that the order of concatenation when doing it with files grouped into folders is based on the file names: numerical first, then alphabetical.
Other Ways to Concatenate Files in Linux
Concatenation commands are great, but there are much faster ways to append files to each other Linux – especially with video, audio, and PDF files. Unlike concatenation commands, however, these can be quite picky with the file type you’re using with them.
Using a Concatenation Program
While it won’t work with text files, FFmpeg will combine audio and video files. As a bonus, it can also convert files to your desired format.
pdftk file1.pdf file2.pdf cat output file3.pdf
Using a Web App
Another way to concatenate files on Linux is through a web app. While they inherently possess a security flaw (a spoofer can steal your data while you send it to the web app, for example), that flaw normally won’t be a big deal for most people who just want to combine pages 1 and 2 in their resumes.
One example is PDF Joiner. This web app allows you to drag up to 20 files into the white box and hit “Join Files” to concatenate them into one.
Some Limits of Concatenation
While incredibly useful, concatenation can lead to some confusing electronic jargon, especially when you merge files of a different format together.
For example: suppose you had two files: “strings.txt” and “image.png.” If you combined them together, you could get something that looked like a corrupt file.
The rule of thumb is to only concatenate files of the same file type, and use an application that can handle that file type.
Frequently Asked Questions
How do I add a file with a space in its name on a Linux command line?
For Linux, you can write files with spaces by putting them in between quotation marks in the command line.
Why do I get a newline between characters when I concatenate text files?
Most text files end with a thing we call a “newline character.” For computers, this is a special character that says, “you’ve reached the end,” which signals text-editing programs to stop parsing text, as there’s no more at the end.
Our latest tutorials delivered straight to your inbox