Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

What capabilities does the "cat" command have?

A reader writes:
I had taken an intro course to UNIX a few years ago, but have found your book Teach Yourself Unix in 24 Hours MUCH more comprehensive and user-aware!

I have hit a stumbling block in Chapter 16 (page 337) where you illustrate the use of cat, however. You show:

$ cat average.awk
NF > 2 { count += 1
        totalsize += $5
}
There is more that follows on this page and page 338. This seems to illustrate that the cat command allows you to input text into the named file. Perhaps that is not what you meant. Did I miss something?

Dave's Answer:

The reader continues with a nice post-script:

I think you book is quite a resource for introducing our people to UNIX. The Information Systems department here in our office has purchased 16 copies and distributed them to my colleagues.

First off, thanks for the kind words about my book. It distills more years of working with the Unix command line than I want to think about (and yet, even this morning I spent two hours working with grep and vi through an ssh connection to my server).

In terms of the cat command, it's really one of the simpler commands available on the command line in Unix/Linux and just dumps out the contents of the specified file to standard output. So by default, cat .login, for example, dumps the contents of the ".login" file to the screen.

Once you start looking at file redirection in the shell, however, then you find out that cat becomes a considerably more powerful utility. For example, if I typed in cat .login > my.login then I would have accomplished essentially the same functionality as a file copy command (e.g., cp). But since cat can accept multiple filenames, a classic use of the command is to merge a bunch of files into a single output filename: cat a.txt b.txt c.txt d.txt > merged.txt.

Another quite common use of cat occurs in shell scripts, where it uses what us shell scripting programmers call here documents. These are a way to trick commands into thinking that you're redirecting input from a file, as in wc < inputfile when there's actually no file being fed to the command. The notation looks like this: << some end marker so if I wanted to find out what wc had to report about a paragraph of text, I could use:

cat << EOF | wc
This is a paragraph of text that we want to learn
more about, so we're feeding it to the 'wc' command
through use of a here document.
EOF
The result of this would be that wc would report how many lines, words and characters were found in its standard input, e.g., the short paragraph of text fed to it through use of the here document.

I hope that clears everything up! If not, please post a response and we can explore this topic further.



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

See i am working on GRIB files (i.e) Grided Binary files.I have gone through a web site where it was me ntioned that if you use this command " cat > EOF " then you will be able to cat all the files there.

So can you please let me know as to how this command works..

Posted by: Abhisek at April 25, 2007 1:15 AM

hello sir, i understood how cat works, but dint get the last part cat <<EOF | wc thing.. And can i get the shell script of cat?

Posted by: sandeep at November 14, 2007 11:31 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]