Did You Know?
Donate to TecMint

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

13 Basic Cat Command Examples in Linux

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

The cat (short for “concatenate“) command is one of the most frequently used command in Linux/Unix like operating systems. cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files. In this article, we are going to find out handy use of cat commands with their examples in Linux.

Linux Cat Command

13 Basic Linux Cat Commands

General Syntax

cat [OPTION] [FILE]...

1. Display Contains of File

In the below example, it will show contains of /etc/passwd file.

# cat /etc/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash

2. View Contains of Multiple Files in terminal

In below example, it will display contains of test and test1 file in terminal.

# cat test test1

Hello everybody
Hi world,

3. Create a File with Cat Command

We will create a file called test2 file with below command.

# cat >test2

Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The text will be written in test2 file. You can see contains of file with following cat command.

# cat test2

hello everyone, how do you do?

4. Use Cat Command with More & Less Options

If file having large number of contains that won’t fit in output terminal and screen scrolls up very fast, we can use parameters more and less with cat command as show above.

# cat song.txt | more
# cat song.txt | less

5. Display Line Numbers in File

With -n option you could see the line numbers of a file song.txt in the output terminal.

# cat -n song.txt

1  "Heal The World"
2  There's A Place In
3  Your Heart
4  And I Know That It Is Love
5  And This Place Could
6  Be Much
7  Brighter Than Tomorrow
8  And If You Really Try
9  You'll Find There's No Need
10  To Cry
11  In This Place You'll Feel
12  There's No Hurt Or Sorrow

6. Display $ at the End of File

In the below, you can see with -e option that ‘$‘ is shows at the end of line and also in space showing ‘$‘ if there is any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.

# cat -e test

hello everyone, how do you do?$
$
Hey, am fine.$
How's your training going on?$
$

7. Display Tab separated Lines in File

In the below output, we could see TAB space is filled up with ‘^I‘ character.

# cat -T test

hello ^Ieveryone, how do you do?

Hey, ^Iam fine.
^I^IHow's your training ^Igoing on?
Let's do ^Isome practice in Linux.

8. Display Multiple Files at Once

In the below example we have three files test, test1 and test2 and able to view the contains of those file as shown above. We need to separate each file with ; (semi colon).

# cat test; cat test1; cat test2

This is test file
This is test1 file.
This is test2 file.

9. Use Standard Output with Redirection Operator

We can redirect standard output of a file into a new file else existing file with ‘>‘ (greater than) symbol. Careful, existing contains of test1 will be overwritten by contains of test file.

# cat test > test1

10. Appending Standard Output with Redirection Operator

Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contains of test file will be appended at the end of test1 file.

# cat test >> test1

11. Redirecting Standard Input with Redirection Operator

When you use the redirect with standard input ‘<‘ (less than symbol), it use file name test2 as a input for a command and output will be shown in a terminal.

# cat < test2

This is test2 file.

12. Redirecting Multiple Files Contain in a Single File

This will create a file called test3 and all output will be redirected in a newly created file.

# cat test test1 test2 > test3

13. Sorting Contains of Multiple Files in a Single File

This will create a file test4 and output of cat command is piped to sort and result will be redirected in a newly created file.

# cat test test1 test2 test3 | sort > test4

This article shows the basic commands that may help you to explore cat command. You may refer man page of cat command if you want to know more options. In out next article we will cover more advanced cat commands. Please share it if you find this article useful through our comment box below.

Ravi Saive

Owner at TecMint.com
Simple Word a Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux.

Your name can also be listed here. Work as a Paid freelancer/writer at TecMint.
Download Free eBooks
Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Ubuntu Linux Toolbox: 1000+ Commands

11 Responses

  1. Why “cat test; cat test1; cat test2″ ?

    Why not “cat test test1 test2″ ?

    Why “cat song.txt | more” instead of “more song.txt”?

  2. Ariel Chiong says:

    Q. Hi All, Can you show me what is the command or terminal to use with this question “Search lines which contains alpha-numeric words( combination of alphabets and number) and copy those lines is sorted order to /root/lines (output should not contain any blank lines)”. for example I have a file called Searchline.txt. thank you.

  3. arush says:

    Can you please elaborate point number 11

    • Braden says:

      #11 is total B.S.

      ‘cat < file' does not take 'file' as an argument.

      What 'cat < file' does, is use the contents (not "contains") of file as the input for cat, taking the place of the keyboard. It is functionally equivalent to 'cat file', and is thus completely redundant.

  4. Excellent information for Linux users..

  5. susheel says:

    i think cat test
    and

    cat < test both are same right?

    if any difference please tell me…..

    • Braden says:

      They are two very different methods of achieving the same result.

      ‘cat file’ takes file as an argument, opening the file and displaying its contents

      ‘cat < file' opens file, and redirects its contents to stdin, so the cat command will take the contents of the file as if they were being typed in by a keyboard.

      To better understand what's going on, try this:

      in terminal 1:

      $ mkfifo test
      $ cat test

      In terminal 2, start typing. Every time you hit enter, you will see the text you just typed appear in terminal 1. Hit Ctrl+D and it will kill both the running cats.

      • Braden says:

        I don’t know why it deleted half my text, but let’s try this again.

        Terminal 1 should have a cat command, and terminal 2 should also have a cat command. If you don’t see two cat commands, then a bot is filtering my comment.

        Terminal 1:

        $ mkfifo test
        $ cat test

        Now you can start typing into terminal 2.

        • Braden says:

          Ah, I figured out what’s going on: It thinks I’m trying to type HTML.

          One more time:

          T1:

          $ mkfifo test
          $ cat < test

          T2:

          $cat > test

          • Braden says:

            Moderators: If you see this chain of self-replies, please fix it and edit the original reply to convey what I’m trying to convey.

            If you didn’t already figure out what’s going on, I used &lt; and &gt; to generate < and >, otherwise, “this <te>xt” becomes “this xt” because it is interpreted as a tag.

            Also, the ability to edit comments would be much welcomed. I hate to mess up a discussion thread with a million self-replies that try to figure out why X didn’t work.

            Thanks

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.