1. Overview

In this tutorial, we’ll look at how to write text into a file using the Linux cat command.

2. The cat Command

The cat command is a utility command in Linux. One of its most commonly known usages is to print the content of a file onto the standard output stream. Other than that, the cat command also allows us to write some texts into a file.

3. The Syntax

Let’s take a look at the general syntax of the cat command:

cat [OPTION] [FILE]

First of all, OPTION is a list of flags we can apply to modify the command’s printing behavior whereas FILE is a list of files we want the command to read.

From the documentation, we can see that if no value is passed for the FILE argument, the cat command will read from standard input. Similarly, it will behave the same when a dash “-” value is passed for the FILE argument. In combination with the Linux redirection operators, we can make the cat command listen to the standard input stream, and redirect the content to a file.

4. Making cat Read From stdin

Let’s execute the cat command:

cat

After we enter the command, we’ll see that the command will not return anything. This is because the cat command is now listening to the standard input.

Let’s try to enter some texts into the terminal:

cat
This is a new line
This is a new line

What we can observe is that whatever texts we’ve entered into the standard input stream will be echoed to the output stream by the cat command. Once we are done, we can terminate the command by pressing CTRL+D.

5. Writing to a File Using cat

To write to a file, we’ll make cat command listen to the input stream, and then redirect the output of cat command into a file using the Linux redirection operators “>”. Concretely, to write into a file using cat command, we enter this command into our terminal:

cat > readme.txt

We’ll see that once again the terminal is waiting for our input. However, this time it won’t echo the texts we’ve entered. This is because we’ve instructed the command to redirect the output to the file readme.txt instead of the standard output stream.

Let’s enter some texts into the terminal, followed by CRTL+D to terminate the command.

cat > readme.txt
This is a readme file.
This is a new line.

The file readme.txt will now contain the two lines we’ve entered. To verify our result, we can use the cat command once again:

cat readme.txt
This is a readme file.
This is a new line.

Voila! We’ve written into a file using the cat command.

5. Appending Text to File Using cat

One thing we should take note of in the previous example is that it’ll always overwrite the file readme.txt. If we want to append to an existing file, we can use the “>>” operator.

cat >> readme.txt
This is an appended line.

To verify that the last command has appended the file, we check the content of the file:

cat readme.txt
This is a readme file.
This is a new line.
This is an appended line.

There we have it: The line we enter is appended to the end of the file instead of replacing the entire document.

6. Here Document

It is also worth noting that the here document syntax can be used with the cat command:

cat > readme.txt << EOF
This is an input stream literal
EOF

where EOF is a token that tells the cat command to terminate when it sees such a token in the subsequent lines.

The token can be any other value as long as it is distinct enough that it won’t appear in the input stream literal. Do note that both of the starting and ending EOF tokens will not show up in the readme.txt file.

7. Conclusion

In this article, we’ve taken a look at the general syntax of the cat command. We’ve also shown how we can make cat command listen from standard input stream instead of a specific file. Finally, we’ve demonstrated how to write or append to a file, using the cat command along with the Linux redirection operators.

guest
0 Comments
Inline Feedbacks
View all comments