Explaining the Linux Command Line: cat
What cat does
In Linux, cat stands for concatenate. What this gem of an application does is take input from the command line and outputs the content to either the screen or to a file. Here’s a simple example.
File1.txt contains the contents “To be or not to be”.
File2.txt contains the contents “That is the question”.
To merge these into File3.txt that contains:
To be or not to be
That is the question
the command cat File1.txt File2.txt > File3.txt would need to be issued.
Or, you could just have the contents of both files displayed on the screen with the command cat File1.txt File2.txt.
As a Linux beginner it might not be obvious how useful this tool is. But once you get deep into using the Linux OS, writing bash scripts, programming applications, etc. you will see how much cat can do for you.
Piping cat to grep
Say you want to cat two files together while searching both texts for certain words. Using our example above, we’ll do a search for the word “be”. To do this you would pipe cat to grep like so:
cat -b File1.txt File2.txt |grep be
This would display the following results:
1 To be or not to be
So you know the word “be” shows up twice, both times in line 1.
Helpful arguments
Here is a listing of some of the handier arguments to use with cat.
b - number the non-blank output lines
n - number all output lines
s - squeeze multiple adjacent blank lines
v - display non-printing characters (except for tabs and end of line characters)
t - same as v but also displays tabs (shown as “^I”)
e - same as v but also diplays end of line characters (shown as "
e - same as v but also diplays end of line characters (shown as “$”)
quot;)
Final Thoughts
Although you have only been shown a basic sampling of what cat can do, you should (with a bit of imagination) see how useful this tool can be. The cat command is one of those tools you will find at the command line, in bash scripts, in configuration files, and in a whole host of other locations. Get to know cat. The time you spend learning it will be time well spent.
This post is part of the series: Linux Command Line
If you ever plan on doing any administration on a Linux machine, you would be well served to get to know the command line interface. In this Bright Hub series you will be introduced to various concepts surrounding one of the most powerful admin tools around.
- Linux Command Line: Introduction
- Linux Command Line: ls
- Linux Command Line: cd
- Linux Command Line: mkdir
- Linux Command Line: df
- Linux Command Line: ln
- Linux Command Line: top
- Linux Command Line: mount/umount
- Linux Command Line: Cron/Crontab
- Linux Command Line: chmod
- Linux Command Line: wget
- Linux Command Line: cat
- Linux Command Line: grep
- Linux Command Line: dd
- Linux Command Line: sudo
- Linux Command Line: startx
- Linux Command Line: adduser
- Linux Command Line: at
- Linux Command Line: aterm
- Linux Command Line: nano
- Linux Command Line: hostname