Why Use a Command Line Instead of Windows?

If you're like many Unix users these days, you probably use a
GUI (graphical user interface) for Unix, with windows and a mouse.
Even Macintosh users now have Unix underneath their gorgeous GUI,
thanks to Mac OS X. While O'Reilly tends to publish many books on the bleeding edge of technology, the latest edition of Learning the Unix Operating System remains true to its roots and emphasizes the Unix command line.

"Aren't command lines for dinosaur
MS-DOS users and wizard system administrators?," you might ask.
"Windows are so much easier!"

Windows are easy--and useful in a lot of cases.
In fact, we've expanded the chapter on windowing (available free online at Chapter 2, Using Window Systems), to cover powerful features like virtual desktops.

But...

Years ago, I took a class on computer user-interface design.
Some of my professor's words have stayed with me since then.
One characteristic of a user-friendly system, he told us,
is that it does what the user wants. In other words, the most user-friendly system isn't necessarily the simplest one.

If you'd never seen a BMW (or a pick-your-favorite-car), you might think that a horse is the best way to get where you're going: it's easy to use, simple to maintain, and you might see some nice scenery as you (slowly) reach your destination. So why would you want a sophisticated automobile?

Command Line Power

$ ls -l | grep "Aug"

If you don't use Unix command lines, that line probably looks like
gibberish. What does it do? It gives you a list of all files that were last modified in August. Maybe you were writing the final files for a year-long project then. Your directory has lots of files;
you need to find a couple of them but can't remember their names.
The command line runs two programs connected by a pipe (which is a lot like a water pipe: it lets data flow between programs). The first program, ls, lists the filenames in your current directory; its -l ("long") option adds details. The second program, grep, filters the output of the first program to show only lines that contain Aug, the abbreviation that ls uses for August.

If that list is still too long and you remember that you did the work in the middle of August, you could narrow the search.
Change the search pattern to match any date between, say,
Aug 10 and Aug 19:

$ ls -l | grep "Aug 1[0-9]"

Figure 1 shows the results of that command line in a Mac OS X Terminal window.

Or, if you want to email the list to someone, you could add another pipe and one more program to the end of the line:

$ ls -l | grep "Aug" | mail joe@foo.xyz

A Mac needs to be configured before the first time you use
command-line mail. Enable Sendmail and remove group-write permission from the root directory.

Why Not a Window?

These command-line examples might seem hopelessly clumsy--until you
try to do that useful job with a typical GUI system. How could you do it? Well, you could open your file manager window. (On a Mac, that's the Finder.) Then you could click down to the directory you want to view. Then you could choose a menu item, or click on a heading, to sort the files by date last modified. (In Finder, click on the "Date Modified" heading.) Your file manager probably can't do a secondary sort to alphabetize that list, though, the way it was sorted on the command line. Too bad. From the command line, you can even add a multi-column sort by typing just 10 or 20 more characters.

And how could you email part of the file manager's list to your coauthor? Maybe you could copy some of the display with your mouse, then open an email window and paste it in. In the Mac Finder, for example, you can shift-click or drag a marquee around the filenames you want to select--as shown in Figure 2--then copy and paste those names into an email. But maybe, as on the Mac, you can only copy the file names, not the file size, date modified, and so on. Maybe the only way to copy the display is by making a screen dump, editing it to show just the files you want,
converting it to a JPEG, and then attaching it to a message.
Or maybe there's no easy way to do this simple operation.

Figure 2. Mac Finder lets you shift-click to select certain filenames

Just like riding a horse, using a graphical interface is fairly easy because it limits you to a few basic operations that you can learn quickly. But if those basic operations aren't what you need, then you may have problems. So, if the programmer who wrote the file manager window didn't think you'd want an alphabetized list of the files you modified in August, or if she just wanted to keep the menus simple, then you're probably out of luck.

This is why it's worth your while to learn to use the Unix command line and Unix windows. Long before Unix did windows, its powerful command-line interface evolved. (It's still evolving, by the way.) The command line lets you string together hundreds of
Unix building-block utility programs. Each of those programs does one little job--such as listing your files, searching for particular text, or sending an email message. The command line gives you the glue to put these programs together in an almost infinite number of ways. Once you've learned the basic operations on a command line, as well as some basic utility programs, you'll have power that window system users can't dream of having (unless those users also happen to be programmers). Even better, by learning the basics of writing shell script programs, you can run a complex command-line operation by typing just one word. (A shell script is basically
a series of command lines pasted into a file.)

We Do Need Windows Sometimes

Of course, a command line can't do everything that windows can do.
For instance, it can be tough to edit photos from the command line.
Browsing Web pages is often a lot easier if you can point to links and
click on them. But Unix lets you do both windows and command lines. It lets you use a command line from inside a terminal window.
In fact, Unix lets you control windows from a command line
(but not the other way around).

Doesn't it make sense to learn what the command line can do--so you'll have a choice? That's what we thought, and that's why we cover the command line in Learning Unix, 5th Edition. I'll also stick my neck out and predict that Learning Unix 6 (and 7, and 8, ...) will cover the command line too--at least until someone designs a window system that lets nontechnical users do what they need to do without having to write a new program.

Learning to use a powerful car does take some effort.
But once you've learned how to drive and you get some practice,
you can do so much more than you could do with a horse.
The same is true of the Unix command line.

Thanks to Chris Stone of O'Reilly & Associates
for his help with this article.