InMotion Hosting Support Center

In this article I'm going to review using the Linux cat command in order to view the contents of files on your server.

To be able to follow with any of the examples in this article, you'll need to have a VPS (Vitual Private Server) or dedicated server with SSH access. More information about this can be found from our article on how to login to your server via SSH.

What is cat?

The Linux cat command doesn't allow you to interact with animals of the feline variety, but it does do a great job at quickly spitting out data from files on your server, and allowing you to display data from multiple files at once.

The reason the command is called cat, is because it's short for concatenate, which typically means linking things together in a chain or series. Using the cat command is very common when interacting with your server, so realizing what all you can do with it is valuable information to know.

What can I do with the cat command?

Below I'll go over several different things you can do with the cat command in order to display data from your files.

cat filename

Using the cat command directly on one file will display the full contents of that file:

cat /home/userna5/access-logs/

In this case we are looking at an Apache access log, so the output would look similar to this: - - [07/Mar/2013:02:45:32 -0500] "GET /about-us HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:02:45:49 -0500] "GET /2013/03/07/today-is-the-day HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:02:45:59 -0500] "GET /favicon.ico HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:02:51:14 -0500] "GET /wp-content/images/slider.png HTTP/1.0" 200 76 "-" "Mozilla/5.0"

cat filename1 filename2

You can also use the cat command on two files in a row, in this example we've already navigated into our /home/userna5/access-logs directory, and now we want to see the visits from two of our websites:


This would show us back to back the data from each file. - - [07/Mar/2013:02:45:32 -0500] "GET / HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:02:45:49 -0500] "GET / HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:02:45:59 -0500] "GET / HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:12:51:14 -0500] "GET / HTTP/1.0" 200 76 "-" "Mozilla/5.0"

cat *

Another way you can run the cat command is with an * asterisk, this denotes any file. So if you were still in your /access-logs/ directory and ran cat * it would simply display the data of all files in the directory.

You can use the * asterisk anywhere in the filename, so for instance cat example* would display data from any filename in the current directory that begins with example:

cat example*

This would show us back to back the data from each file. - - [07/Mar/2013:02:45:32 -0500] "GET / HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:02:45:49 -0500] "GET / HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:02:45:59 -0500] "GET / HTTP/1.1" 200 76 "-" "Mozilla/5.0" - - [07/Mar/2013:12:51:14 -0500] "GET / HTTP/1.0" 200 76 "-" "Mozilla/5.0"

cat >> newFile

Another intersting thing you can also do with the cat command is also using it to insert data into a new file, or append new data to a file.

Using cat >> newFile will start allowing you to insert data into a file called newFile, or if that file already exists append the new data onto the end. After you're done typing in the data to place inside the file, you'll want to hit Ctrl-D on your keyboard to write the data to the file:

cat >> newFile
This is a new file

I'm creating on the fly

Using the cat command!

Now if we simply cat the file we created we can see the new data we just added:

cat newFile
This is a new file

I'm creating on the fly

Using the cat command!

cat -n, cat -b, cat -s

The cat command also has a few flags that allow you to change how data is displayed from files. For these examples I've created a simple file called newFile and it has the following contents when you just run cat newFile alone:

cat newFile

You get back:




cat -n

Using cat -n will allow us to see the line numbers of the file:

cat -n newFile

Here you can see we have 10 lines total:

1 user001
2 user002
3 user003
4 user004
6 user005
9 user006
10 user007

cat -b

Using cat -b will only count non-blank lines:

cat -b newFile

So you can see while we have 10 total lines, we only have 7 with data on them:

1 user001
2 user002
3 user003
4 user004

5 user005

6 user006
7 user007

cat -s

Using cat -s will supress multiple blank lines to a maxium of one:

cat -s newFile

Here you can see the two blank lines after user005 have been replaced with only one now.:




cat has a backwards cousin tac!

As a bonus there is also a less known command called tac which operates just like cat but as the letters suggest, in reverse.

tac newFile

Will give you back:




The tac command doesn't have the additional options of cat, but it can come in very handy for certain tasks.

A good example of when the tac command shines, is if you had an extremely large log file, and you were looking for some specific text in the log using a command like grep. Just using the grep command alone would start at the very beginning of that log file, possibly going through very old data that isn't relevant to what you're looking for. So if you instead used the tac command like in the following example this can help you save time:

tac bigLogFile | grep Error

Once you've found enough data, you can hit Ctrl-C on your keyboard to stop the tac command from reading further up the specified file.


You should now have a clear understanding of what you can accomplish with the Linux cat command, and how it might come in handy while managing your server.


Was this article helpful?

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Forum Login

You are NOT logged in. You can still browse our Support Center.

To participate within our Community Support Forum:

n/a Points
2014-12-23 3:04 am


i have .cat,.inf,.sys, how to edit ............

that file is limited time file i need unlimited time.......

43,761 Points
2014-12-23 9:37 am
Hello Manju,

The cat command it a viewer command. It does not allow editing of a file. To do that, you would use an editor like nano or vi, or vim. These allow you to make changes to the content of a file.

Kindest Regards,
Scott M

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

2 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!