Linux cat Command

In this Linux tutorial you will learn how to use the Linux cat command. The Linux cat command is the Unix command to list a file’s contents onto your screen, or pass via pipeline to use with other Linux commands. The cat command comes from the word concatenate.

The usage of the Linux cat command is cat [options] [file].

There are quite a few options you can use with the Linux cat command, but I rarely use any of them on a regular basis. The only option I use with the cat command on a regular basis is cat -n or cat --number.

For an example of using the cat command, we’ll say I have a file named Linux-Friends.txt and in this text file I have some friend’s names, phone numbers, and e-mail addresses that run Linux. So now I want to get someone’s phone number from this list. I can run cat Linux-Friends.txt in a Linux terminal and I will get the contents of this file listed on my screen.

Now if I wanted to see how many friends I have listed in this file, I can have cat number the lines for me by using the cat -n option. So if I run cat -n Linux-Friends.txt the cat command will then output each of the Linux friend’s that I have listed and place a number in front of each one so I can see how many friends I have. Using cat -n [file] is also useful when doing some programming or coding, so you can see what line number you need to edit.

You can also pipe off of the Linux cat command to use it with other Linux commands. So for this example of the Linux cat command I want to only grab a certain person’s name from the list to display their contact information. So I can run cat Linux-Friends.txt|grep Brandon and now I will only get Brandon’s contact information.

Another example of using the Linux cat command with other Linux commands via pipe is if I only want Brandon’s email address to display and not his phone number. The Linux-Friends.txt file is in this format: last name, first name – phone number – email address so I can use the Linux cat command with grep and also awk to display only what I want. So I can run cat Linux-Friends.txt|grep Brandon|awk '{print $4" "$5}' and now I have Brandon’s phone number.

For an explaination of the command above I will show each output as each command is ran.
cat Linux-Friends.txt
–friend 1,2,3–
Stimmel, Brandon – (330) 222-7222 –
–friend 5,6,7–
Now we add |grep Brandon
Stimmel, Brandon – (330) 222-7222 –
and you see the other friends are now gone. Now we can add |awk '{print $4" "$5}' which will display the 4th and 5th column, and the – in that file counts as a column as well. The 4th column is (330) and the 5th column is 222-7222. So my output now would be: (330) 222-7222

I hope this has helped you learn how to use the Linux cat command in a Linux terminal and also using the cat command with other commands via pipeline. Remember to come back and visit Beginner Linux Tutorial for all of your Linux questions and tutorials. If there is a Linux tutorial you’d like to see on Beginner Linux Tutorial please let us know.

9 thoughts on “Linux cat Command

  1. ANkush,
    You can edit files with vi, vim, pico, nano, gedit, kate, and tons more. Any text editor you like, whether it’s GUI or CLI. The ones I mentioned are usually packaged with most Linux distributions.

  2. explanations simple but wonderful getting more interested in working with linux

  3. the Linux cat command will display the entire file at once so if the file is 20 pages long you get the entire file at once. The Linux less command will display a page worth and you can scroll the rest by hitting enter.

  4. Yes there are pirated versions of Windows out there, but Linux is still more stable, faster, and more secure. So pirated version of Windows still won’t solve those issues.

    Plus for companies, they can’t use pirated versions of Windows.

  5. If money is an issue just get yourself a pirated version of Windows. I do this all the time and it works great!

Leave a comment

Your email address will not be published. Required fields are marked *