What is the cat command in Linux & how to use it?

The cat command is very useful in Linux and Unix-like operating systems. Also cat is a short form of the concatenating which means link (things) together in a chain or series.

How to get help for the cat command

I always suggest reading the manual page and help page. So, It will give you an in-depth introduction to the command.

So to get the manual page type the below command,

man cat

But, to get the only command options, you can type the below command.

cat --help

Syntax of the cat command

Now you know how to get help about the cat command. So, let’s talk about cat syntax. The syntax of the cat command is as below,

Syntax: cat [OPTION] [FILE]…

You can view the content of a file, and you can also create a single file or multiple files with the help of the cat command. So, below I’m going to discuss how can you create a file and view the content.

Creating a file using cat command

For instance, I’m going to create a new file with the name of amit. In this file, we will type the famous quotes of Bruce Lee. And after completing the message, we will press ctrl+d to close and save that message.

cat > amit

Empty your mind, be formless, shapeless — like water. Now you put water in a cup, it becomes the cup; You put water into a bottle it becomes the bottle; You put it in a teapot it becomes the teapot. Now water can flow or it can crash.

To view the above message using cat command

cat amit

So, here amit is the file name.

As a result, you can see in the below picture,

cat command in linux
viewing a file amit using cat command

To view the multiple files

You can also see the multiple files at once.

cat file1 file2

To view contents of a file preceding with line numbers

You can also specify the line number which you want to see. So, you can see the syntax below. Also, you can find an example below.

cat -n file-name

For example, If you want to see the first line of the page, you type the below command.

cat -n 1 amit

Copy the content from one file to another file

if you want to copy the message of one file to another file then you can use it for that as well. So, you can use the below command.

cat source-file-name > destination-file-name 

Conclusion

In this article, you learned about the cat command in Linux, and you can see that it is easy to use and you get a lot of information. So, I hope you understand, but if you have any questions, you can ask in the comment section.

Also, you may like to read about

Leave a Reply

Your email address will not be published.