linux command cat in linux, unix with examples
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.
![]() |
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.
$cat [option] [filename]
Simple cat command
![]() |
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.
cat command open file
![]() |
cat to open a file |
$cat file_Name
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 |
$cat fileName1 fileName2
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
![]() |
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 cat -b 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 |
$cat -n fileName
Now the next command is cat -n 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 |
$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 cat -s 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 |
$cat -E fileName
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