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/example.com

In this case we are looking at an Apache access log, so the output would look similar to this:

123.123.123.123 - - [07/Mar/2013:02:45:32 -0500] "GET /about-us HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [07/Mar/2013:02:45:49 -0500] "GET /2013/03/07/today-is-the-day HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [07/Mar/2013:02:45:59 -0500] "GET /favicon.ico HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [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:

cat example.com example2.com

This would show us back to back the data from each file.

123.123.123.123 - - [07/Mar/2013:02:45:32 -0500] "GET /example.com/test1 HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [07/Mar/2013:02:45:49 -0500] "GET /example.com/test2 HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [07/Mar/2013:02:45:59 -0500] "GET /example2.com/test1 HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [07/Mar/2013:12:51:14 -0500] "GET /example2.com/test2 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.

123.123.123.123 - - [07/Mar/2013:02:45:32 -0500] "GET /example.com/test1 HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [07/Mar/2013:02:45:49 -0500] "GET /example.com/test2 HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [07/Mar/2013:02:45:59 -0500] "GET /example2.com/test1 HTTP/1.1" 200 76 "-" "Mozilla/5.0"
123.123.123.123 - - [07/Mar/2013:12:51:14 -0500] "GET /example2.com/test2 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:

user001
user002
user003
user004

user005


user006
user007

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
5
6 user005
7
8
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.:

user001
user002
user003
user004

user005

user006
user007

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:

user007
user006


user005

user004
user003
user002
user001

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.

Conclusion

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.

 

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

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

News / Announcements

WordPress wp-login.php brute force attack
Updated 2014-07-17 06:43 pm EST
Hits: 200941

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

Need more Help?

Search

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: support@InMotionHosting.com
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!