Command Line
From BenningtonWiki
Contents |
If you're using a Macintosh, find Terminal in Applications / Utilities and run it. You may want to put it in the Dock, as you'll be using it a lot.
[edit] The Prompt
When you first run Terminal, you'll get a line that looks like this:
cs0:~ code$ _
The first word is the computer name. For these examples, I ssh'd into cs0 and so it's showing cs0. The tight-lipped smiley :~ is the current path, and don't worry about it right now. 'code' is the account name that you logged in with. The '$' is reminding you that this is the Bennington education you paid for.
This is the Terminal prompt. It's waiting for you to type something. What you type are commands, always followed by pressing return. The commands do things. Often the commands will print stuff out. When the commands are done you get the prompt again. Welcome to the interactive virtual world of 1975.
[edit] Commands and Parameters
The reason the other command line tutorials suck is because they lead you through exercises in "becoming super user" and "editing your cron jobs" (whatever) and garbage that has no place in an introduction. I like to start with banner:
cs0:~ $ banner Message: _
Type any old message and press return. And then stand back. Cool, huh? And you were beginning to think command lines didn't have graphics. (You may want to make your Terminal window wider; a standard width back then was 132 characters.)
banner is a command. Its raison d'etre in the good ol' days was to impress visitors who were touring the million dollar corporate computer center. I had a banner printout on greenbar paper when I was twelve. It said "STAR WARS".
Most commands need additional data to do their thing. banner, for example, needs to know what message to print out. Most commands will let you type the additional data on the same line before you press return. This additional data is called the command's parameters (or alternately arguments).
cs0:~ $ banner hello, world
If you get into trouble with a command you can usually type control-c to stop it. For example, if you give a ridiculously long parameter to banner you can interrupt it by typing control-c. That returns you to the prompt. I dare you to try it now.
Often commands have default behavior when you don't specify any parameters. For example, the default behavior for cal prints out a calendar for the current month:
cs0:~ $ cal
February 2008
S M Tu W Th F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29With parameters, you can print any month you want:
cs0:~ $ cal 12 2008
December 2008
S M Tu W Th F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31Hey, New Year's Eve is a Wednesday night this year. Hmmm. Try cal with just a single number parameter.
banner and cal are two of about five thousand commands you can type at the command line prompt. Here are some ultra-simple ones to try out now:
date whoami bc -l uptime finger clear echo hello, world
echo is obviously banner's bastard sibling. Here are some that aren't so simple:
top ifconfig curl -h
[edit] Command Options
A lot of commands also have command line options that can be used to modify the command's behavior. The options are always prefixed with - or -- and used like this:
cs0:~ $ date -u Mon Feb 18 03:53:46 GMT 2008 cs0:~ $ date Sun Feb 17 22:53:59 EST 2008
See how -u made date print out the date and time in Universal Time? Without the option, it prints out the time in the local time zone. Commands have default behavior regarding options, just like they do with parameters. The distinction between options and parameters is kinda blurry and you don't need to worry about it. Just understand that some things that you type after a command are prefixed with - and that this usually tweaks the command's behavior somehow. Here are some of the commands with options. Try to understand what they're doing based on their output:
cal -j date -r 946702799 banner -w 40
[edit] File System Commands
So far we've just seen a bunch of sissy commands that do nothing useful, really. It's time to get serious. A lot of the commands have to do with the file system; that is, your files and directories on disk. Want to see the files in a directory? Use ls. It has lots of options, but the most useful is -l, which prints out a detailed list.
cs0:~ $ ls hello.rb cs0:~ $ ls -l total 8 -rw-r--r-- 1 code users 58 3 Jun 14:48 hello.rb
There are commands to copy (cp), delete (rm), rename (mv), display (cat) and anything else you could dream of doing with or to a file.
[edit] Man Pages
Heck, instead of telling you how to use the file system commands, I'll let the command line do the explaining itself with the most useful command of them all: man. It prints out a built-in manual page for any command that you give it.
cs0:~ $ man cal
These man pages all have a common format, showing a brief description of the command, a synopsis of its arguments and options, and a detailed description of everything the command can do. Press q to get back to the prompt -- that's important to remember, and the up and down arrow keys are used to scroll up and down. Check out man ls. I told you it has a ton of options. The hard thing is finding a list of commands. man can't do that. But there are lots and lots of lists on the web of useful Unix commands. Pick a command, use man on it, try it out.
