How to Concatenate Files in Linux

Combine Files In Linux Feature Image File1 Hi There File2 How Are You Doing

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:

  • cat
  • join
  • paste
  • sed

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"
Linux Mint Terminal Cat File1 Txt File2 Txt File3 Txt

It can be done with other commands as well:

  • join command:
join file1.txt file2.txt > file3.txt
Terminal Join Concatenate File1 Txt And File2 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 command:
paste -d "" file1.txt file2.txt > file3.txt
Terminal Paste Concatenate File1 Txt And File2 Txt 1
  • sed command:
sed -e "r file2.txt" file1.txt > file3.txt
Terminal Sed Concatenate File1 Txt And File2 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:

  • Using cat command:
cat dog_folder/* > output_file.txt
Terminal Cat Concatenate Contents Of Dog Folder
  • Using paste command:
paste -d '' dog_folder/* > output_file.txt
Terminal Paste Concatenate Contents Of Dog Folder
  • Using sed command:
sed -s '' dog_folder/* > output_file.txt
Terminal Sed Concatenate Contents Of Dog Folder

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.

Use PDFtk to merge PDF files. See the following example:

pdftk file1.pdf file2.pdf cat output file3.pdf
Pdftk In Terminal Pdftk Page1 Page2 Page7 Combining To Become 7 Page Resume Pdf Right After Installation

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.

Pdfjoiner Uploading Three Pdf Files

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.

Notepad Concatenated Text And Png File Opened On Notepad
A concatenated TXT and PNG 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.

Terenz Jomar Dela Cruz
Terenz Jomar Dela Cruz

Terenz is a hobbyist roboticist trying to build the most awesome robot the world has ever seen. He could have done that already if he wasn't so busy burning through LEDs as a second hobby.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox