Writing Text to File Using Linux Cat Command


Introduction

The Linux cat command is a powerful utility that allows users to concatenate, view and create files. One of its useful features is the ability to write text to a file, either by appending it to the end of the file or by overwriting the existing content of the file. In this article, we'll explore how to use the cat command to write text to a file in Linux, covering the various options and flags that can be used to customize the behavior of the command.

Introduction to the Linux Cat Command

The cat command is a simple yet versatile command that is commonly used in Linux to perform various file operations. It's short for "concatenate" and can be used to join multiple files together or to display the contents of a file on the screen.

The basic syntax of the cat command is as follows −

$ cat [options] [file]

The options argument is optional and specifies various flags that modify the behavior of the cat command. The file argument is the name of the file you want to manipulate. If you don't specify a file, cat will read from standard input (for example, keyboard input). Here are some common examples of using the cat command −

To view the contents of a file on screen −

$ cat file.txt

To concatenate multiple files and display the resulting result on the screen −

$ cat file1.txt file2.txt file3.txt

To concatenate multiple files and save the resulting output to a new file −

$ cat file1.txt file2.txt file3.txt > newfile.txt

As you can see, the cat command is a useful tool for viewing and manipulating text files in Linux. In the next section, we will look at how to use the cat command to write text to a file.

Write Text to a File Using the Cat Command

To write text to a file using the cat command, you can use the echo command to output the text and pipe it to the cat command. The echo command is a built-in shell command used to display a message on the screen or to write a message to a file.

Here's an example of using the echo and cat commands to write "Hello world!" in a new file called hello.txt −

$ echo "Hello, World!" | cat > hello.txt

The echo command outputs the string "Hello, World!", and the | The symbol (called "pipe") redirects the output of the echo command to the input of the cat command. The > prompt redirects the output of the cat command to the hello.txt file, overwriting the file if it already exists or creating a new file if it doesn't exist.

You can also use the echo and cat commands to add text to the end of an existing file. To do this, you can use the “>>” symbol instead of the > symbol.

For example, to add the text "This is a new line" to the end of the “hello.txt” file, you could use the following command −

$ echo "This is a new line" | cat >> hello.txt

Here is the output you would see if you ran this command −

Hello, World!
This is a new line

You can also use the -n flag with the echo command to suppress the newline character at the end of the output. This can be useful if you want to write multiple lines to a file without adding extra blank lines between them.

For example, to write the following three lines to a file named “lines.txt” without adding any extra blank lines between them −

This is line 1.
This is line 2.
This is line 3.

You can use the following command −

$ echo -n "This is line 1." | cat > lines.txt
$ echo -n "This is line 2." | cat >> lines.txt
$ echo -n "This is line 3." | cat >> lines.txt

This will write the three lines to the “lines.txt” file as follows −

This is line 1.This is line 2.This is line 3.

You can also use the cat command to directly write text to a file, without using the echo command. To do this, you can use the cat command in combination with the > or >> symbol and the << symbol, followed by a delimiter string. For example, to write the following three lines to a file named “direct.txt”:

This is line 1.
This is line 2.
This is line 3.

You can use the following command −

$ cat > direct.txt << EOF
This is line 1.
This is line 2.
This is line 3.
EOF

The << symbol indicates the start of input, and the string EOF (which can be any string you choose) indicates the end of input. The cat command will write everything between the << and EOF symbols to the “direct.txt” file.

Conclusion

In this article, we have seen how to use the cat command to write text to a file in Linux. The cat command is a powerful and versatile utility that can be used for a variety of tasks, including concatenating files, viewing file contents, and writing text to a file. Using the various switches and flags available with the cat command, you can customize its behavior to meet your specific needs.

Updated on: 17-Jan-2023

41K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements