When I started learning about the Bourne shell this semester in my Intro to Linux class, I discovered cat. No, you can't get a whole book out of it, though this interview (with the accompanying proposed O'Reilly cover) was my favorite April Fool's Day joke.
However, cat is actually pretty useful. Short for "concatenation," it directs standard output of a file or a command to the terminal. The most basic use of cat is to type the command at the bash prompt, followed by a line of text. Here's what happens:
$ cat print this line of textThe command line is "standard input." The line that prints out to the shell is "standard output."
print this line of text
Use cat to quickly view files.
OK, maybe that's not incredibly useful, at least for our purposes. But, say, what if you have a really messy home directory, with a lot of practice files? Like this:
DEADJOEYou could go into an editor like joe or pico and open each file individually--kind of the way you have to do with most GUI word processing/text editing applications. Or you could do it much more quickly with cat:
$ cat soopersekritThat's all! I think I was trying out the joe editor with this file. Nothing to see here. But this could be really important later--say, what if you're investigating an intrusion incident and you're going through a huge number of files and directories to see if there's a nasty little rootkit script hidden away there somewhere? That can speed things up tremendously, although the security engineers I know probably use scripts that automate that process. For longer files you want to use | (pipe) and the more filter.
ok, here's a soopersekrit file that I'm going to copy.
seems pretty intuitive.
$ cat jeoffrey | moreThis will show you a page of a file at a time--like the one above, "For I will consider my cat Jeoffry," a long, affectionate paean from harmless 18th-century religious lunatic Christopher Smart to his feline companion. (I like to use it for playing with long files, because it's a charming poem, and because it seems appropriate to use with cat.) Just press the spacebar to proceed.
Use cat to redirect output from one file into another.
You could use cp or mv to do this, but you might find cat more expedient. Here you use the redirect output symbol (>):
$ cat fun_with_cat > fun_with_cat_backupAlways be very careful with this command, though, because if you try to redirect output into an existing file, it will replace any content in that file. To safeguard against this, you can enable a feature called noclobber, which displays an error message and won't permit the command to be executed.
Use cat as an editor.
Want to get something down quickly, without leaving the command line or opening up a new shell? Just use cat to start sending input to a new file:
$cat > notes_on_catWhen you're done, control-d saves and exits the file. But as with redirecting output, be careful that if you return to the same file that you don't end up overwriting what you've previously stored there. To pick up where you left off, use the append output symbol (>>):
Things I can do with cat:
1. Preview files quickly.
2. Redirect the output from one file into another. (careful!)
3. Use cat as an editor.
$ cat >> notes_on_catCat does have limitations as an editor, though--you can't go back and edit text, and you can only delete the line you're working on. So while I first attempted to draft this entry entirely in cat, I found it a wee bit impractical. But for taking notes while playing around with the Linux shell, it's incredibly useful. There are lots of other things you can do with cat involving pipes and tees and filters, but these are the three I've found most helpful lately. I'm hoping to add more of the useful things I've gleaned from my classes here--it actually provides something of a review for me and keeps me in shape as a technical communicator.
OK, what was I saying? Cat is a very useful command.