Concatenating Files in Linux
Sometimes, we need to do some operations that require using multiple files at the same time. This can be something as common as searching for some text in multiple files or merging multiple files into a new one.
In this quick tutorial, we’ll show some useful operations that can make our life easier when concatenating files in Linux.
2. The cat Command
The most frequently used command to concatenate files in Linux is probably cat, whose name comes from concatenate.
The command syntax follows the form:
cat [options] [files]
In the next sections, we’ll dig deeper into the command and the options we can use.
3. Displaying a File
This displays myfile in the standard output:
This is a text file.
4. Creating a File
It’s as easy as using the redirection operator:
cat > newfile
After that, we can start typing what we want to add to the file:
creating a new file.
When we want to save the file, we have to press CTRL+D. Notice that if the file exists, it will be overwritten.
5. Concatenating Files
The most simple concatenation is to display multiple files in the standard output:
cat file1 file2
The command above displays the files sequentially:
My file 1 My file 2
We can also use wildcards to display all the files that match a common pattern:
So far, we’ve been displaying the files in the standard output, but we can write the output into a new file:
cat file1 file2 > file3
Also, we can append a file to an existing file:
cat file1 >> file2
Another useful option is to read from the standard input, which we represent by using ‘-‘ :
cat - file1 > file2
Then, we can type the text we want to concatenate before file1:
text from standard input
Now, if we type cat file2 to display the file, we can see the text we’ve introduced concatenated with file1:
text from standard input My file 1
Also, we could append the standard input after the file instead of before:
cat file1 - > file2
If we go a bit further, we can also concatenate the output of any other command to cat:
ls -la | cat > file1
Finally, we can pipe cat output to other utilities to create more powerful commands:
cat file1 file2 file3 | sort > file4
In this case, we’ve concatenated three files, sorted the result of the concatenation, and written the sorted output to a new file called file4.
6. Other Options
cat --help Usage: cat [OPTION]... [FILE]... Concatenate FILE(s) to standard output. With no FILE, or when FILE is -, read standard input. -A, --show-all equivalent to -vET -b, --number-nonblank number nonempty output lines, overrides -n -e equivalent to -vE -E, --show-ends display $ at end of each line -n, --number number all output lines -s, --squeeze-blank suppress repeated empty output lines -t equivalent to -vT -T, --show-tabs display TAB characters as ^I -u (ignored) -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB --help display this help and exit --version output version information and exit
For instance, we can use the -n option:
cat -n myfile
That displays the number of each line:
1 This is a test file. 2 It contains multiple lines.
Alternatively, we can use -e:
cat -e myfile
In this case, it shows a $ at the end of each line:
This is a test file.$ It contains multiple lines.$
These are just some quick examples that show how to use these options.
We covered the basics pretty quickly to focus later on the file concatenation.
And, we’ve also seen that cat can be handy when combined with other commands and can be used in many different situations.