cat command in Linux with examples.

Let’s learn about the cat command in Linux with examples and options

Cat command is basically used for,

to view the content of the file,

can create a new file,

also, we can append the content of the file,

and put the content or append into another file.

How to VIEW the CONTENT of A SINGLE file.

Command:
# cat first_file 

So, while using the cat command we can check the content that our file contains and we have the file named “first_file”.

How to VIEW the CONTENT of A MULTIPLE files.

Command:
# cat first_file second_file third_file 

So, now we have to see the content of Multiple files for that we have a three file with the name “first_file, second_file, and third_file”.

How to VIEW the NUMBER of LINES by using cat command.

Command:
# cat -n check_the_number_of_line 

So, suppose we have to check the number of lines for the file, then we will use -n option with the cat command.

And we have the file named “check_the_number_of_line” and with that, we have used -n option to check the number of lines.

How to CREATE a NEW FILE using cat command in Linux.

Command:
# cat > create_a_new_file 

So, to create a new file by using cat command we will use > redirection sign.

After that put your content as you want and press control + d key from the keyboard to get out from the terminal

How to APPEND the CONTENT in a file.

So, now we have to append the content in an existing file let’s look out the example.

Command:
# cat >> create_a_new_file 

So, to append the content we have used >> sign with the cat command and then we can start writing our content

When the content part has been written just press control + d key to come out from the terminal.

How to APPEND a FILE CONTENT into ANOTHER FILE.

Now we have to add a whole content of a file into another file, we will use >> sign.

Command:
# cat second_file >> third_file 

So, we can see that there are two files second_file and the third_file, and we have put all the content of second_file into the third_file.

How to PUT the CONTENT into a NEW file.

In this example, we will copy our whole content into the new file by using a > sign.

Command:
# cat create_a_new_file >> copy_the_content 

So, now we can see the content in create_a_new_file and we have copied the whole content in copy_the_content file.

How to COPY THE CONTENT of MULTIPLE File a SINGLE File.

Suppose we have a multiple files and we have to copy into a single file, then we will use > sign

Command:
# cat first_file second_file third_file > all_in_one_file 

So, there are three files first_file, second_file, and third_file we have to copy all the content into a single file.

How we can CHECK the END of LINE.

Command:
# cat -E check_the_number_of_line 

Suppose we have to check whether the line has been ended or not, so we will -E option.

So, at the end of a line it will show a $ sign while using -E option.

How to SUPPRESS the EMPTY LINES by cat command in Linux.

Command:
# cat -s suppress_file 

So, what to do if we have more empty lines in a file and don’t want in our output.

So, we will -s option that will suppress the empty line of the file.

A cheat sheet for a cat command
How to VIEW the CONTENT of A SINGLE file.

Syntax:
#cat [name_of_the_file]

Example:
#cat tastethelinux
How to VIEW the CONTENT of A MULTIPLE files.

Syntax:
#cat [file1] [file2] ........ [filen]
 
Example:
#cat file1 file2 file3
How to VIEW the NUMBER of LINES by using cat command.

Syntax:
#cat -n [file_name]
 
Example:
# cat -n tastethelinux
How to CREATE a NEW FILE using cat command in Linux.

Syntax:
#cat > [file_name]
 
Example:
# cat > create_a_new_file          
//Once done with the content press control + d 
How to APPEND the CONTENT in a file.
Syntax:
#cat >> [file_name]
 
Example:
# cat >> create_a_new_file
//Once done with the content press control + d
How to APPEND a FILE CONTENT into ANOTHER FILE.

Syntax:
#cat [append_file] >> [file_name]
 
Example:
# cat second_file >> third_file
How to PUT the CONTENT into a NEW file.

Syntax:
#cat [file_name] >> [new_file]
 
Example:
# cat create_a_new_file >> copy_the_content
How to COPY THE CONTENT of MULTIPLE File a SINGLE File.

Syntax:
#cat [file1] [file2]....... > [new_file]
 
Example:
# cat first_file second_file third_file > all_in_one_file
How we can CHECK the END of LINE.

Syntax:
#cat -E [file]
 
Example:
# cat -E check_the_number_of_line
//At end of every line it will show us $ sign
How to SUPPRESS the EMPTY LINES by cat command in Linux.

Syntax:
#cat -s [file]
 
Example:
# cat -s tastethelinux

So Guys lets ready for Quiz

0%

Can we append the file by using a cat command?

Correct! Wrong!

Can we create a folder by using a cat command?

Correct! Wrong!

-n option is used for?

Correct! Wrong!

Can we view the multiple files using cat command?

Correct! Wrong!

Which command is used to append the file

Correct! Wrong!

cat command QUIZ are you ready to play?
Thanks for your valuable time!
To Learn more Basic command you can go to "LINUX BASIC COMMAND SECTION" and Spread the Knowledge Keep Supporting us! Follow us on our ID's: >> Instagram: @tastethelinux_official >>Facebook: https://fb.me/tastethelinuxofficial >>Twitter: @LinuxTaste

Share your Results:

To learn about ls command refer this link.

Also, refer a Practice link.

Thanks to read the POST keep supporting us.

Give your valuable time