Special offer
popup-offers
English
+370 5 205 5502 sales@monovm.com

The Best CAT Commands

In today's Linux tutorial, we will show you a few different CAT commands and how to use them.

29 Jun, 21 by Antoniy Yushkevych 6 min Read

CAT here means 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 single 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 for you. Have a look!

Here is a syntax in order to use the cat command. 

 

How to create a new file?

Say, you want to create files named, A.txt and B.text. Now, follow these steps to reach the conclusion.

Go on the terminal window and write the following command:

cat >A.txt

After this, type any text as you want. 

Now, press the Ctrl key and press d.

Repeat the same process for the other file named B.txt

cat >B.txt

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, 

 

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. 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

 

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

 

How to append standard output by using Redirection Operator?

If you want to append a file in an already existing file, then 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.

 

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!

 

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 this command is for you:

cat a1.txt b1.txt > c1.txt

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

 

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, interesting, right?

tac a1.txt

 

How to append the text to an existing file?

To begin with, use this command first,

cat >> a1.txt

Now, to add 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.

 

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 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.

 

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 command:

cat -t a1.txt

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

 

How to remove the blank lines?

In order to discard any unwanted blank lines, use this command:

cat -t a1.txt

 

How to highlight the end of the line?

To highlight the end line of your content in the file, follow the following command:

cat -E ‘filename’

That’s it!

 

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

Use the following command, and reach the solution:

cat *.txt

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

 

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

This will definitely solve your query.

 

How to open dashed files?

To open some dashed files, use this command:

cat - - ‘-dashfile’

You will get the entire data of -dashfile.

 

How to merge the contents of more than one file?

To merge multiple files, here is a simple 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. 

 

Conclusion

We hope you have a better grasp of the commands of Linux. In the Linux world, the above-stated commands are generally the most widely used commands. They are simple and make your work easier. You must have noticed that!

Practice these 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.

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