Advertisement
Tech

Explaining the Linux Command Line: cat

Picture this: You have two text files you need to merge into one so you open both documents and copy paste the contents of one into the other. Imagine being able to do this without having to open a single file. That is what cat does. This article will show you how to merge two files with cat.

By jlwallen
Desk Tech
Reading time 3 min read
Word count 527
Linux Computing Linux commands
Explaining the Linux Command Line: cat
Advertisement
Quick Take

Picture this: You have two text files you need to merge into one so you open both documents and copy paste the contents of one into the other. Imagine being able to do this without having to open a single file. That is what cat does. This article will show you how to merge two files with cat.

On this page

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”.

Advertisement

File2.txt contains the contents “That is the question”.

To merge these into File3.txt that contains:

Advertisement

To be or not to be

That is the question

Advertisement

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.

Advertisement

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:

Advertisement

cat -b File1.txt File2.txt |grep be

This would display the following results:

Advertisement

1 To be or not to be

So you know the word “be” shows up twice, both times in line 1.

Advertisement

Helpful arguments

Here is a listing of some of the handier arguments to use with cat.

b - number the non-blank output lines

Advertisement

n - number all output lines

s - squeeze multiple adjacent blank lines

Advertisement

v - display non-printing characters (except for tabs and end of line characters)

t - same as v but also displays tabs (shown as “^I”)

Advertisement

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 “$”)

Advertisement

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.

Advertisement

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.

  1. Linux Command Line: Introduction
  2. Linux Command Line: ls
  3. Linux Command Line: cd
  4. Linux Command Line: mkdir
  5. Linux Command Line: df
  6. Linux Command Line: ln
  7. Linux Command Line: top
  8. Linux Command Line: mount/umount
  9. Linux Command Line: Cron/Crontab
  10. Linux Command Line: chmod
  11. Linux Command Line: wget
  12. Linux Command Line: cat
  13. Linux Command Line: grep
  14. Linux Command Line: dd
  15. Linux Command Line: sudo
  16. Linux Command Line: startx
  17. Linux Command Line: adduser
  18. Linux Command Line: at
  19. Linux Command Line: aterm
  20. Linux Command Line: nano
  21. Linux Command Line: hostname
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement