Using the Linux cat command

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.

 

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

2 thoughts on “Using the Linux cat command

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

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X