Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

In this tutorial, we’ll learn how to append the contents of multiple files into one. The commands demonstrated will work on any POSIX-compliant terminal, including bash.

2. Using Only the cat Command

The cat command is short for concatenate. Using cat, along with shell redirection, we can append the contents of the files that are passed to the command as an argument. Let’s take a look at how this is done with a simple example. First, let’s display the contents of three individual files:

$ cat file1.txt
Contents of file1
$ cat file2.txt
Contents of file2
$ cat file3.txt
Contents of file3

Next, we’ll “cat” the files together into one file:

$ cat file1.txt file2.txt file3.txt > file.txt
$ cat file.txt 
Contents of file1
Contents of file2
Contents of file3

We combined all three files by simply specifying the file names following the cat command and redirecting standard out to a new file.

3. Using cat in Combination With the find Command

We can also use the find command in combination with the cat command to produce a similar result. This will allow us to perform a more reliable, and more powerful, search for files to concatenate into one. First, let’s list the contents of the current directory and the contents of the subdirectory within it:

$ ls
file1.txt file2.txt fileSubDirectory
$ ls fileSubDirectory
file3.txt

Now let’s build out our find command, invoke the cat command, and then redirect standard out into a file called file.txt:

$ find . -type f -name 'file*.txt' -exec cat {} + >> file.txt
$ cat file.txt
Contents of file1
Contents of file2
Contents of file3

The result is one file containing the contents of all three of the files. Using the find command allows us to recursively capture all of the text files, including one found in a subdirectory. Here, we used the -type option with the value f to find only files (not directories).

We then specified which files to search using the -name option followed by a filename pattern. In addition, we used the -exec option to invoke the cat command on the files found. From there, we redirected the output into one file.

4. Concatenate With the paste Command

We can also use the paste command with the -s option, along with shell redirection, to produce the same result:

$ paste -s file1.txt file2.txt file3.txt > file.txt
$ cat file.txt
Contents of file1
Contents of file2
Contents of file3

The -s option, short for serial, allows us to print each file one at a time instead of the default — in parallel.

5. Conclusion

In this quick tutorial, we demonstrated how to easily append the contents of multiple files into one. To find out more about these commands, take a look at our catpaste, and find tutorials.