Cat (short for concatenate) is used to concatenate files and print their output on the screen.
In this article, we are going to show you some examples of cat command. All the commands have been executed on RHEL 9.
Syntax
Concatenate command has following syntax.
Cat [OPTIONS] [FILES]
Following are some of the important options of cat command.
Option | Description |
---|---|
-n | Print line number with output |
-s | Do not show repeated empty lines |
-T | Separate the tab with ^I character |
-E | Display $ at the end of each line. |
Examples
Show the content of a file on terminal
This is one of the basic examples of cat command. In this example, we are going to show you the content of a file on the terminal. Suppose the file is sample.txt file and is located in current working directory. The command is as follows:
cat sample_file.txt
Copy the content of a file to another file
You can use the cat and redirect operator (>) to copy the content of one file to another file. The complete command should be as follows.
cat old_file.txt > new_file.txt
If the file is not located at the destination, it will be created and if the file is located it will be overwritten.
Append the content of one file to another file
You can use the (>>) operator to append the content of one file to another file. Suppose the files are file_1.txt and file_2.txt. You should run:
cat file_1.txt >> file_2.txt
Display line numbers
You can display the line number with file content using -n option. Suppose the file is file_2.txt, the command should be executed as follows:
cat -n file_2.txt
Do not show repeated empty lines
You can use the -s option to make cat command do not show empty lines in the output. The complete command is as follows:
cat -s file_2.txt
Separate the tabs with ^I character
You can use -T option to separate the tab with ^I character:
cat -T file_1.txt
Display $ at the end of each line
You can display $ at the end of each line by using -E option. The command should be executed as follows:
cat -E file_2.txt
Concatenate multiple files and print the output on terminal
With the help of cat command, you can concatenate multiple files and print the output on terminal.
The command is as follows:
cat file_1.txt file_2.txt
Instead of concatenating two files displaying the output on terminal, you can write the output to another file say file_3.txt. If the file is not located at the destination, it will be created otherwise overwritten.
You should run the following on terminal.
cat file_1.txt file_2.txt > file_3.txt
Append the output of multiple files to another file
If you want to append the output to the third file file_3.txt use >> operator instead of >. You should run:
cat file_1.txt file_2.txt >> file_3.txt
Create a file
Instead of any text editor, you can create a file with the help of cat command and > operator. The command is as follows.
cat > sample_file.txt
You will be prompted to type your content on the terminal. When you are done press ctrl + D from the keyboard to save the file and quit.
Similarly, you can use >> operator to append the content to a file as follows.
cat >> sample_file.txt
Conclusion
Thank you for reading the article. We will be more than happy if you have examples of cat command. Send us and we will add to this article.