Linux cat command

CAT Command on Linux with examples

Today we will learn about the Linux cat command. CAT stands for “Concatenate”. Whether you are an academic or a student, you must be aware of this term. This is a very common and widely used command in the coding world.

As for Linux or Unix commands, this command is not new.

First, let us introduce you the meaning of CAT.

What is the Linux CAT command?

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

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

Moving on, here are some prerequisites first.

  • Linux system
  • Terminal window or command line

List of cat commands in Linux with examples

Now, we present some commonly used CAT commands in Linux with examples and output screens to help you get familiar with them. For you. Take a look!

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

How to create a new file?

You can create files named A.txt and B.text in Linux if you want. Now, follow these steps to arrive at the conclusion.

Step 1: Go to the Terminal window and enter the following command:

cat >A.txt

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

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

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

cat >B.txt

Step 5: Write your desired content again and press Ctrl + d.

you did it!

Moving on, to view the contents of a single file, here’s the syntax:

If you want to see the contents of a file, check out our following command:

cat filename.txt

Here, you have the freedom to display the contents of a single file as well as multiple files. It is up to you.

To view contents of multiple files, use:

cat file1.txt file2.txt

How to view content with line numbers in a file?

Here are the commands:

cat [options] filename(s)

The ‘options’ word allows you to add a set of instructions to your cat command in Linux. Now, what kind of instructions?

Let us tell you.

For example, use the -n option to view content with numbered lines.

cat -n filename

This command lets you display content with numbered lines.

Output:

  • Welcome to Linux
  • Enjoy coding

How to use more and fewer cat commands?

There are examples where the file size is too large. In such cases, the content does not fit in the terminal window. Here, you can use these parameters to fix the issue.

Here are the commands:

cat a.txt | more

cat b.txt | less

How to add standard output using a redirection operator?

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

cat a1 >> a2

This will help to associate the contents of the a1 file with the contents of the a2 file.

How to redirect content of a single file in Linux?

The contents of the file are further placed into a file.

 cat a1.txt > b1.txt

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

Now, when you type the following command,

cat b1.txt

You can view the contents of b1 file a1!

How do you redirect content belonging to multiple files?

If you want to redirect multiple file contents to a single destination, type this command:

 cat a1.txt b1.txt > c1.txt

Now, file c1 contains the aggregate contents of both files; a1 and b1.

How to display content in reverse order?

The cat command can help you display the contents of a file in reverse order. Use tack, which is the opposite of cat

tac a1.txt

How to add text to an existing file?

To start, first, use the following command,

 cat >> a1.txt

After adding the required text, type it, for example:

Press Ctrl + d

Now you check the file named a1, and you can see the updated file.

How to combine different operations?

cat a1.txt b1.txt > c1.txt
cat c1.txt

The order in which you specify the files in the command is important. The files are shown in the destination file in that specified order.

How to show TAB-separated lines?

If you’re looking for a way to display the contents of a file with tab spaces, use the following cat command:

 cat -t a1.txt

All tab spaces appear as symbols: ^I.

How to remove blank lines?

To discard any unwanted blank lines, use the cat command below:

cat -t a1.txt

How to highlight the end of the line?

To highlight the last line of your content in the file, follow the cat command listed below:

cat -E ‘filename’

How to display the contents of each text file in a folder?

Use the commands listed below, and find the solution:

cat *.txt

Using it, you can see all the contents of files existing in your folder.

How to delete repetitive empty lines seen in the output?

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

cat -s a1.txt

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

How to open the dashed file?

To open any dashed file, use this command:

cat - - ‘-dashfile’

You will get the complete data of -dashfile.

How to merge the contents of multiple files?

To merge multiple files, you need to follow the cat command given below:

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

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

Conclusion

We hope you can better understand the CAT command in Linux with examples. In the Linux world, the CAT commands described above are generally the most used Linux commands. They are simple and easy in your work. You must have noticed that inside the vehicle reading the article! Practice these basic CAT commands on your console and try running them in code. We hope that the information provided above has added value to your knowledge and broadened your knowledge base. Buy Linux VPS From Oudel Inc.

Scroll to Top