7 Examples of Cat Command in Linux
Cat command is used to display the content of file(s) on a standard output which is a terminal screen. It is one of the widely used commands. You can read more about cat command on its Wikipedia page.
In this post, I will teach you how you can use this command with examples on a Linux machine.
Here, we go.
The syntax of the command is as follows:
cat [option(s)] [file_name(s)]
You can use the following options with this command.
|-n||Used for displaying the line numbers of a file|
|-e||Used for displaying $ character at the end of lines of a file|
|-s||Used for suppressing repeated empty lines|
|-T||Used for visually displaying the tabs and spaces in a file|
Displaying the content of a single file on terminal
We will start with the basic example to teach you about the cat command.
I have created a file “testing_file.txt” in my current working directory and I want to output its content on my terminal screen.
Write the command on your terminal as shown below and hit enter from your keyboard.
Below is the output.
If a file is not located in your current working directory, you can specify the path as follows.
Following is the output of the above command.
Displaying the content of multiple files on the terminal
You have seen how you can display the content of a single file. If you have multiple files and you want to display their content on terminal, input their names to cat command.
I have created two more files testing_file2.txt and testing_file3.txt.
Run the command as follows.
cat testing_file.txt testing_file2.txt testing_file3.txt
The above command should return the following output on your screen.
Displaying line numbers
If you want to display the line numbers along with the output of the file, you can use the -n option as shown in the following number.
cat -n testing_file.txt
You should see the following output on your screen.
Writing the content to a file
In this example, we will see how can we write the content to a file instead of displaying it on the terminal by using redirect or pipe > operator. It is just like copying the content of old_file.txt to new_file.txt.
For the sake of this example, I have a file old_file.txt and I want to output or write the content to a file new_file.txt.
cat old_file.txt > new_file.txt
A command should return the output as shown.
Merging or concatenating the content of files
Cat command can be used to merge the content of files. Suppose, I have two files old_file.txt and new_file.txt. I want to merge and combine the content of both files and write the content to a new file combined_file.txt using > operator.
cat old_file.txt new_file.txt > combined_file.txt
Now, you have merged the content and written to a file combined_file.txt. You can output on your terminal screen as follows.
Following is the output you will see on your screen.
Appending the content of a file
You can append the content of one file at the end of another file using >> operator. We will use the files old_file.txt and new_file.txt.
Run the command as shown below.
cat old_file.txt >> new_file.txt
As soon as the command runs, the content of old_file.txt will be appended at the end of new_file.txt.
You should see the following output on your terminal.
Displaying an end of lines of the file with $ character
You can use the -e option to display the end of lines of a file with $ character. Run the command in the following fashion.
cat -e testing_file.txt
You will observe that every line of a file will be ending with $ character.
Thank you for reading this post. These were some of the important examples of cat command. It can also be used in combination.
If you want to learn about echo command, click here.