13 Basic Cat Command Examples in Linux

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux course in 5 Days

We are thankful for your never ending support.

Ravi Saive

I am Ravi Saive, creator of TecMint. 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. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

Receive Your Free Complimentary eBook NOW! -

Download Free Linux eBooks

Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Introduction to Linux - A Hands on Guide

You may also like...

20 Responses

  1. cecille says:

    can somebody please explain this code below – the line that starts with cat. it seems like the code does not work since our report still prints as portrait, which may be the default printer setup for orientation. we even manually triggered the code but it still did not work. your help is appreciated. thanks so much…

    ISP_PRNTR=mcsprt ; export ISP_PRNTR

    #Extract the last 3 characters (file extension)
    ext=`echo $1|awk -F . ‘{print $NF}’`
    #Extract the file name without extension
    file=`basename $1`
    file=`echo $file|sed ‘s/\.[^.]*$//’`

    cat ${1} | /usr/bin/acroread -toPostScript | lp -d $ISP_PRNTR -o landscape

    • Aaron Kili K says:

      From the code:

      This part of the code has not been used in the last command that prints the file, though it only deals with the file name which should not affect the layout during printing.

      #Extract the last 3 characters (file extension)
      ext=`echo $1|awk -F . ‘{print $NF}’`
      #Extract the file name without extension
      file=`basename $1`
      file=`echo $file|sed ‘s/\.[^.]*$//’`
      

      The variable file has not been used in command;

      cat ${1} | /usr/bin/acroread -toPostScript | lp -d $ISP_PRNTR -o landscape
      

      Instead the original filename, $1 has been used.

      It could be a problems of printer specific options that are required.

  2. satishchow says:

    while viewing large file using cat command. how to move immediately to end of the file and how to exit from the file. I used Cntrl+d. But couldn’t exit. and If I need to edit the file, how can I edit using command [vi].

  3. satish says:

    cat test* >>some.txt

  4. Sieyongdong says:

    thank u so much for sharing this ! It’s very useful.

  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

  6. Excellent information for Linux users..

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

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

  9. Why “cat test; cat test1; cat test2” ?

    Why not “cat test test1 test2” ?

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

Got something to say? Join the discussion.

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

Join Over 95000+ Linux Users
  1. 77,543
  2. 4,213
  3. 35,218

Enter your email to get latest Linux Howto's