writing
teaching
speaking
photography
consulting
biography
weblog
contact
header-right2 home home bio

Wicked Cool Linux

|<     Column 1: Unlocking the power of redirection     >>
By Dave Taylor

This column originally appeared in the August, 2004 issue of LinuxWorld Magazine and is reproduced here under the Creative Commons license. Learn more.

In a lot of ways, Linux seems pretty similar to other modern operating systems, with overlapping windows, a mouse, games, an email program that talks to Outlook, an application that can read and write Microsoft Word documents, etc. What's different, though, is that Linux is built atop a very powerful underlying kernel that supports a wide variety of hardware and offers all users access to its power without any of the graphical layers that get in the way of real power users.

That's what this column is all about: cracking open the shell, learning how you can really exploit the power and capabilities of the command line, and how you can dip your toe into the water and learn about programming basics through the painless world of Linux shell scripting. Come on in: the water's just fine...

REDIRECTION

One of the greatest capabilities of the shell, one of the truly great inventions of Unix (then reimplemented in Linux) is file redirection. But redirection offers quite a bit more power than you may be used to.

The most basic redirection is to use > to point the output to a specified filename. This looks like this:

ls > my-files

This saves the output of the ls command to a file called "my-files". If you want to add something else to the file, this is known in Linux circles as appending content, and that's done by using >> rathern than just a single angle bracket:

date >> my-files

This adds the output of the date command to the existing contents of "my-files". Be careful, though; it doesn't know how to add a few blank lines between content blocks or anything else. It's crude and direct, literally opening up the file and streaming in the content.

You can also redirect input, which is very useful too. For example, to find out how many words are in the new file, use the wc command with the '-w' flag:

wc -w < my-files
	48

This shows that there are 48 "words" in the file (in this case digits with spaces around them are also counted as words, so it's not that useful a result in this particular instance).

MORE SOPHISTICATED USES

A more sophisticated use of shell redirection is to use what's known as a here document, which is best explained by demonstrating it:

wc -w << ENDMARK
	This is a test of what I would
	ordinarily have in a file but am
	instead streaming from within
	this script.
ENDMARK
	21

Can you see what's happened here? I've used a here document to make an instant file that I've then fed to the wc command. Once the command has processed its input, though, the file contents vanish and nothing's left behind.

This proves to be a tremendously useful technique in shell scripting because we can, for example, create 3-5 line mini-scripts for specific Linux commands within a shell script, so we don't have to worry about a bunch of files all being available when they can easily be created on-the-fly.

These are the basics of file redirection, at least until we consider the difference between stdin, stdout, and stderr, which we'll explore in the next column. In the meantime, thanks for reading and please email me with any and all questions you have or topics you'd like to see me discuss here!

Dave Taylor is a 25-year veteran of Unix, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Creating Cool Web Sites, among his sixteen technical books. His main Web site is at http://www.intuitive.com/