Linux command cat in linux with examples

Ansal Sajan
5 min readJan 6, 2021

Hello, guys welcome. In this session you will learn how you can use “cat” command in Linux. “cat” is one of the most frequently used command on linux.

cat command in linux
linux command cat

It has Three related functions with regard to text files one is displaying the text while the second is combining copies of text files and third is creating a new text file . So let’s see how we can use cat command so I will just press ctrl + Alt + T to open my terminal.

Now the syntax for using cat command is you just need to write cat and then you give so just give some options and then you give the file name

$cat [option] [filename]

Simple cat command

linux command cat
linux command cat

$cat

Let’s see how we can use cat command so First of all, you can just give cat and press ENTER and you can see it’s giving us this kind of cursor so we can just type For example, “hello world” and then press ENTER and it will echo it again so you can see this is the basic functionality without any option for the cat command but the cat is not only the echoing program it can do much more than echoing something.

So to exit this cat command you can just press Ctrl + D which means the end of the file and you are out of this cat command.

cat command open file

cat command to open file
cat to open a file

$cat [fileName]

For example, I have a file called list1.txt, okay and I want to print this text file on my terminal so I can use a cat and then the name of the file and press ENTER and it will display me the content of the file.

cat command to open multiple files

linux cat command combine files
linux cat command combine files

$cat [fileName1] [filename2]

Now, For example, I want to show the content of two files so you can see here I have list1.txt and also have

So for example, I want to display the content of both the files I can just add one more file name list2.txt and press ENTER and then it will combine the content of both the file and display me on this terminal.

So the first use of cat command you can say is for displaying the content of a file or multiple files.

prints numbers in the output files

Print numbers
prints numbers in every line except blank

$cat -b [fileName]

Now there are some options related to cat command. For example, I want to show the line number on every line of the content of the file. I can use a which will add line number to non-blank lines okay. I will just give the name of my file list1.txt and press ENTER.

And you can see it adds the line number to the non-blank line so you if you remember my list1.txt have one blank line here right and it will skip or it will not add the line number to the blank line but all the lines which have sub-content it will add the line number to them.

cat command to print numbers in all lines

print numbers in all lines
print numbers in all lines

$cat -n [fileName]

Now the next command is and what it will do is it will add line number to all the lines so For example, I will just say list1.txt and press ENTER and now it has added the line number to the blank line also.

cat command to eliminate extra line breaks

eliminate extra line breaks
eliminate extra line breaks

$cat -s [fileName]

Now for example I have a text file which has multiple lines breaks something like big line breaks. And I want to display the content of the file and I want to remove these all big line breaks. I would just want to show only one line break then I will just say and then the name of the file list1.txt and press ENTER

So there can be many blank lines but it’ll squeeze all the blank lines and gives only one blank line. And it will not affect the content of the file. Remember it’s not changing the content of the file but only displaying it in our convenient manner.

cat command to add dollar symbol

prints dollar symbol end of each line
prints dollar symbol end of each line

$cat -E [fileName]

Now I will just give and then I will just give and press ENTER. It adds the dollar ($) symbol at the end of each line. So you know that this is the end of the line.

Conclusion

So these are the basic commands and option related to cat command now if you want to know more options related to the cat you can enter man and then just enter cat and press ENTER and then you will be able to see there are more options related to cat command.

I just showed you the most important options with cat command so just use the man command to read or learn more about this cat command.

The cat command can also be used to create new files. But this we will see in the next session. I will show you how redirection works using command line okay.

So this third functionality of cat we will see in the next session. So I hope you enjoyed this tutorial please rate your feedback in comment and bye for now.

THANKS, HAVE A NICE DAY

Related,

Originally published at https://www.protechguidez.com on January 6, 2021.

--

--

Ansal Sajan
0 Followers

Welcome to PRO TECH GUIDES, the perfect place to explore Technology, Games, Apps, Linux systems and commands, Shell Scripting, Termux Tools and much more.