Displaying File Contents with cat


Displaying File Contents with cat

Instead of using more to display files, you can use cat (as in "concatenate"), which displays files but does not pause so you can read the information. Instead, it displays the file or fileswhich whiz by onscreenand leaves you looking at the last several lines of the file (Code Listing 1.8).

Code Listing 1.8. With cat, long files whirl by, and all you'll see is the bottom of the file. You can also redirect cat output to a file, as shown at the end of the listing.

[jdoe@frazz jdoe]$ cat newest.programs.txt ... xpmtoppm* xpp* xpstat* xrfbviewer* xscreensaver.kss* xvminitoppm* xwdtopnm* xxd* yaf-cdda* yaf-mpgplay* yaf-splay* yaf-tplay* yaf-vorbis* yaf-yuv* ybmtopbm* yelp* yes* ypcat* ypchfn* ypchsh* ypmatch* yppasswd* ypwhich* yuvsplittoppm* yuvtoppm* z42_cmyk* z42tool* zcmp* zdiff* zeisstopnm* zforce* zgrep* zipgrep* zipinfo* zless* zmore* znew* [jdoe@frazz jdoe]$ cat newer.programs.txt   newest.programs.txt > all.programs [jdoe@frazz jdoe]$ 

The cat command also lets you redirect one or more files, offering a function that some versions of more do not.

To display file contents with cat:

  • cat newest.programs

    To begin, type cat plus the filename (probably not newest.programs unless you're naming your files just like we are).

    The file contents will appear onscreen; however, if the file is longer than a single screen, the contents will whirl by, and all you'll see is the bottom lines of the file the 24 or so that fit on a single screen.

    Or

  • cat newer.programs newest.programs

    You can also specify multiple files for cat, with each file being displayed in the order specified. In this example the contents of newer.programs will zip by, then the contents of newest.programs will zip by.

    Or

  • cat newer.programs newest.programs >  all.programs 

    In this example, we've added a redirection symbol (>) plus a new filename. This tells Unix to print out both files; however, instead of displaying the files onscreen, it redirects them to the file called all.programs. Aha!Here's where cat does something better than more. See Redirecting Output, earlier, for more information about redirecting commands.

Tips

  • If you inadvertently use cat with a binary file (a non-text file), you might end up with a whole screen of garbage. On some systems, you might try stty sane to fix itmore on this in Fixing Terminal Settings with stty in Chapter 3. You could also just close your terminal window and log in again to fix it.

  • The tac command is just like cat, but backward. Try it! Oddly handy, eh?

  • You can also view file contents using the more command. See the previous section for details.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net