A Quick Look At The Cat Command

If you want to view the contents of a text file in Linux, you have something called the ‘cat’ command. Being able to use the cat command is important. This article will give you a quick look at the cat command. After all, this was written during the weekend and a quick article seems like a good idea.

It’s possible to view text in the terminal. Many files are just that, able to be read as text. This isn’t true for all files, of course. Trying to read a .tar.gz file as text is unlikely to be fruitful. On the other hand, a .sh file would be trivially read as plain text.

When you’re manipulating text in the terminal, you want a tool like Nano. When viewing text in the terminal, you can use a tool like the cat command. It’s a handy tool.

The cat Command:

You shouldn’t have to install anything. If you enter cat --version in the terminal, you’ll get an output like this:

As you can see, the cat command is a part of the GNU core utilities and, as such, should be installed on almost all Linux systems by default. This means you won’t need to install anything.

If you check the man page, with man cat, you’ll see that it’s described like so:

cat – concatenate files and print on the standard output

Other than the big word concatenate (which means to link files together), you can see that this is the correct tool for the job. What you type in is standard input and what comes out in the terminal is standard output. (That’s often referred to as stdin and stdout, respectively.)

As we’re going to be taking a quick look at the cat command, you might as well understand what it’s for and what it’s expected to do.

Using The cat Command:

As suggested above, this requires an open terminal. Press CTRL + ALT + T and your terminal should open. That’s not always true, but it’s true more often than not.

With your terminal now open, let’s start with something you should have – that is a ~/.bash_history file. If you don’t have a ~/.bash_history, pick a different text file that’s in your current directory (PWD) by using the ls command to search for such. You might have a ~/.bashrc for example.

Now, let’s just view that ~/.bash_history file in your terminal.

If you’re like me, that’s a great deal of text. You’ll need to scroll up to view all of the text, but the cat command is pretty helpful like this.

Let’s say you wanted to create a new file. You may want to read this article:

How To: Write Text To A File From The Terminal with “>” and “>>”

Using the information from that, you can use the cat command to create a new file with this command:

So, try the following:

You should see that there’s now a file named ‘foo’ in your PWD.

There’s a lot one can do with this cat command. You can show the contents of more than one file at a time, like so:

Using the same concepts from above, you can even use > or >> to take the output from cat and insert it into another file.

Finally, and cat has so much more to offer – but this is just a quick look – you can tell cat to number the lines in the output. For example:

See? Nice and easy! The cat command will happily show the output with line numbering enabled. This stuff is easy enough to commit to memory because the cat command is surprisingly powerful for such a little command.

Closure:

So, yeah… It’s a weekend. I have written some larger articles during the past few weeks. That means an easy day is always welcome. Don’t forget that you can make it easier on me by writing an article for the site! I do welcome contributions and it doesn’t matter much if it’s something I’ve already covered – so long as you do a better job than I did!

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.