CAT Command in linux with examples [Updated] - 2024

Are you looking for the best CAT command in linux with examples? You are at the right place to get the best CAT command examples guide.

Updated: 29 Jan, 24 by Antoniy Yushkevych 7 Min

List of content you will read in this article:

CAT is an abbreviation of “Concatenate”. Whether you are an educator or a student, you must be aware of this term. It is an extremely common and widely used command in the coding world.

When it comes to Linux or UNIX commands, this command is no newbie. 

First, let us introduce you to the meaning of CAT.

CAT command helps to create the single file as well as multiple files, concatenate them, and redirect the output in the files or terminal itself.

Additionally, it displays the content of one or more files without actually opening them for editing. 

Moving on, here are some prerequisites first.

  • Linux system
  • Terminal Window or Command line

Now, we are presenting some commonly used CAT commands in Linux with examples and output screens too which will help you get familiar with it. for you. Have a look!

Here is a syntax in order to know how to use the CAT command in Linux? 

cat

1. How to create a new file?

If you want to create file in Linux named, A.txt and B.text. Now, follow these steps to reach the conclusion.

Step 1: Go on the terminal window and write the following command:

cat >A.txt

Step 2: After this, type any text as you want. 

Step 3: Now, press the Ctrl key and press d.

Step 4: Repeat the same process for the other file named B.txt

cat >B.txt

Step 5: Again type your desired content and press Ctrl + d. 

You’re done!

Moving on, In order to look at the contents of a single file, here is the syntax:

Let us look at the following command if you want to see the contents of a single file:

cat filename.txt

Here, you have the liberty to display the contents of a single file as well as multiple ones. It’s up to you. 

In order to view the contents of multiple files, use:

cat file1.txt file2.txt

Moving on, 

2. How to view the contents with line numbers in a file?

Here is the command:

cat [options] filename(s)

The term ‘options’ helps you to add a set of instructions to your cat command in linux. Now, what kind of instructions?

Let us tell you. 

For instance, use -n option to see the content with numbered lines.

cat -n filename

This command helps you to show the content with numbered lines.

Output:

  1. Welcome to Linux
  2. Enjoy coding

3. How to use More and Less by cat command?

There are instances where the size of the file is too large. In such cases, the content doesn’t fit in the terminal window. Here, you can use these parameters to solve the issue.

Here is the command:

cat a.txt | more

cat b.txt | less 

4. How to append standard output by using Redirection Operator?

If you want to append a file in an already existing file, then you can use the following command:

cat a1 >> a2

This will help to append the contents of the a1 file to the contents of the a2 file.

5. How to redirect the contents of a Single File in Linux?

The contents of the file are put further in a file. 

cat a1.txt > b1.txt

In this case, the contents of the a1 file will be redirected to file b2. However, if the destination file is not mentioned, it will be automatically created.

Now, when you type the following command,

cat b1.txt 

You will see the contents of the a1 file in b1!

6. How do you redirect the contents belonging to multiple files?

If you want to redirect the contents of multiple files to a single destination, then type this command:

cat a1.txt b1.txt > c1.txt

Now, the c1 file contains the overall content of both the files; a1 and b1.

7. How to display the content in reverse order?

The cat command can help you to display the content of the files in reverse order. Use tac, which is the reverse of cat

tac a1.txt

8. How to append the text to an existing file?

To begin with, use the following command first,

cat >> a1.txt

after adding the required text, type it, for instance:

All you have is now

Press Ctrl + d

Now that you check the file named a1, you will see the updated file.

9. How to combine several operations?

If you want to combine the output of several files, and put that into a new file, use the following cat command:

cat a1.txt b1.txt > c1.txt

cat c1.txt

The order in which you mention the files in command matters. Files are shown in that specific order in the destination file.

10. How to show TAB-separated lines?

If you are hunting for a way to display the contents of a file with tab space, use the following cat command:

cat -t a1.txt

All the tab spaces appear in the form of this symbol: ^I.

11. How to remove the blank lines?

In order to discard any unwanted blank lines, use the following cat command:

cat -t a1.txt

12. How to highlight the end of the line?

To highlight the end line of your content in the file, follow the bellow listed cat command:

cat -E ‘filename’

That’s it!

13. How to display the content of every text file in a folder?

Use the bellow listed command, and find the solution:

cat *.txt

By using this, you will see all the contents of the existing files present in your folders.

14. How to erase repetitive empty lines seen in the output?

There are times when some unwanted empty lines are seen in the output, to get rid of them, here is a solution for you:

cat -s a1.txt

after running this cat command listed above you solve your problem.

15. How to open dashed files?

To open some dashed files, use this command:

cat - - ‘-dashfile’

You will get the entire data of -dashfile.

16. How to merge the contents of more than one file?

To merge multiple files, you have to follow below-given cat command:

cat ‘filename 1’ ‘filename 2’ ‘filename 3’ > “merged_filename”

After merging the contents of all the files, the combined data will be represented in the merged file. 

We hope you have a better grasp of the CAT command in Linux with examples. In the Linux world, the above-stated CAT commands are generally the most widely used Linux commands. They are simple and make your work easier. You must have noticed that inside the article vehicle reading it!

Practice these basic CAT commands on your console, and try to execute them in the code. We hope that the information provided above-added value to your knowledge and widened your knowledge base.

People are also reading:

Antoniy Yushkevych

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com