In this blog we'll look at the popular Linux command called cat. Which is often used to view files.

It's typically used to view a single file. But the primary purpose of cat (when it was created) was to actually concatenate files (display the content of multiple files).

We'll see that cat also has a few other interesting use cases:

  • It can be used as a sort of command-line "Notepad" to quickly write some text and save it into a file.
  • It can be used to "glue" together multiple files (merge the content of multiple files).

And we'll look at options that we can pass to the cat command to make the content easier to read:

  • Add line numbers, which can be helpful when we view text files containing code.
  • Remove unnecessary empty lines from the output, so that we can squeeze more content on a single screen.

How to View a File on Linux

It's super easy to display the contents of a single file on Linux. All we have to do is run a command like this:

cat name_of_file

In some cases, we'll want to enhance this output. Let's see a few ways we can do that.

cat with Line Numbers

Let's imagine we take a look at this file:

Maybe we want to tell someone:

Hey, there's a missing semicolon ; on the "cout" line.

Sometimes, that's not ideal, as "cout" could appear on multiple lines.

Better way to do this: Tell the cat command to display line numbers. So that we go from this:

To this:

And now it's easier to pinpoint the problem. We just say "Hey, there's a semicolon missing on line 5."

To tell the cat command to display line numbers all we have to do is add the -n option:

cat -n name_of_file

Remove Consecutive Empty Lines

Some files will have a lot of consecutive empty lines. When we display such files the screen gets filled up with useless empty space. Which makes it harder to read the whole thing without scrolling up and down.

Consider this file:

Note how much screen space we need just to display 5 lines of text. Well, we can solve this.

We can tell cat to remove consecutive empty lines, with the -s option:

cat -s name_of_file

If we do this with our previous "bad file," we get this output:

It's much more condensed, making it easier to read without unnecessary scrolling.

What Does the Linux Command cat Stand For?

"cat" is short for concatenate. Because cat is actually an application that can take the content of multiple files, and display them all in one shot, together.

When you take file1, file2, and file3, and display them all at once, that's called concatenation.

What Does the cat Command Do in Linux?

The cat command can be used in many ways.

Use cat to View a Single File

The most common use case is to run a cat command to view a single file. Here's me looking at three separate text files, with three separate cat commands:

cat groceries.txt
cat bills.txt
cat reminders.txt

Use cat to View Multiple Files (Concatenation)

Instead of looking at each file, one by one, we can use a single command and display all three files, combined (concatenated):

cat groceries.txt bills.txt reminders.txt

We just had to enumerate the files we wanted to read:

  • groceries.txt
  • bills.txt
  • reminders.txt

And this leads us to the third use case of cat.

Use cat to Merge Multiple Files into a Single File

We can use the cat command to "glue" together multiple files.

In our last command, cat displayed the contents of three files.

All we have to do now is tell our command interpreter to save this unified content into a new, single file. And we do that with the > redirect operator.

By adding >unified.txt at the end, we tell our command interpreter:

Hey, take the output generated by this command, cat groceries.txt bills.txt reminders.txt, and save it into a new file called unified.txt.

So if we have three files:

  • groceries.txt
  • bills.txt
  • reminders.txt

And we want to merge their contents, and save it into a new file called unified.txt, we can run a command like this:

cat groceries.txt bills.txt reminders.txt >unified.txt

If we now take a look at the unified.txt file, we'll see that it includes the content of all three files:

cat unified.txt

Create a File on Linux with the cat Command

An interesting use of the cat command is to create files. It's pretty much a trick / shortcut when we need to quickly type something and save that content to a file. Here is how to do it.

First, we type this command:

cat >name_of_file

For example, if we'd want to save our content to a file named quicknote.txt, we'd run this:

cat >quicknote.txt

Next, we start to type our text:

We typed our first line. If we want to add multiple lines of text in our file, we just press Enter to jump to the next line:

And then start to type content for the new line:

When we finish with the last line we want to add, we press Enter, and then immediately press CTRL+D to save our file.

To show this in action, here's how it looks like when we want two lines in our file. Note how after pressing Enter we jump to line 3:

Here, we type nothing. We just press CTRL+D. And this will close this "editor" mode of the cat command; and the lines we typed will be saved to the quicknote.txt file.

When using this, note the subtle difference in syntax (the extra > character).

To display the contents of a file, with the cat command:

cat name_of_file

To save content to a file, with the cat command:

cat >name_of_file

So there's an extra > redirect operator in there when we want to save content to a file.

How this works is that it makes the cat command wait for input from the user (instead of taking that input from a file). And, normally, cat would take that input we provided and display it on the screen. But with >name_of_file we tell our command interpreter:

Hey, don't display this content on my screen. > Redirect (save it) to name_of_file instead.

And that's how we end up with the content we just typed saved into that file. And how we can use the cat command as a sort of "command-line Notepad."

Learn More

If you love Linux, or you need to learn more about it to advance your career, check out our Linux course for beginners:

Learning Linux Basics Course & Labs | KodeKloud

Already familiar with Linux basics? Then check out our Linux learning path for more advanced courses.

Thank you for reading, and see you in the next blog!