Command line

Revision as of 15:26, January 9, 2007 by 07djd (talk | contribs) (Next commands: added link to Wikipedia's longer article)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The command line is the old-fashioned way of talking to a computer: you type a command, press Enter, and wait for the results. There are several ways to access a command line:

  • In Windows, go to Start -> Run... and type "cmd.exe"
  • In Mac OS X, open the program Terminal (in /Applications/Utilities)
  • In Linux, press Alt-Ctrl-F1
  • SSH or Telnet to Unix
  • SSH to WSO

The command line can be confusing at first, but it can be much more efficient and powerful than a graphical interface (saving you, for example, from having to restart your computer after using Autohost). Mastering the command line is the first step to becoming a computer wizard, particularly a Linux wizard, not to mention a WSO root.

First commands

A fundamental concept of the command line is the working directory. Just as Mac Finder or Windows Explorer window shows you the current folder, every command line has a directory associated with it that you are somehow "in". By default it's your home directory. To see what directory you're currently in, type

pwd

To change the directory you're in, type

cd DIRECTORY

but instead of DIRECTORY, type the one you want to go to. How do you know what directories are available? Just type

ls -l

to see the contents of the current directory. You'll see output like this:

 -rw-r--r--     1 evan  evan        1482 Feb  6  2005 word_parse.pl
 drwxr-xr-x    22 evan  evan         748 May  5  2005 wso

If there's a "d" instead of a "-" in the first character position, it's a directory; otherwise it's a file you're looking at. (Hey, who needs icons??) Toward the end of the line, you'll see the last time the file was modified, and finally the name of the file or directory. Don't worry about that other crap for now. Anyway, if you want to "cd" into a folder you see, just type "cd" and then the name of the folder you see. That's called the relative path, because you could have lots of folders with the same name scattered about your hard drive, but the shell will know to operate on the folder in the working directory. The absolute path, on the other hand, is unique to every file, and might be something like /home/john/Documents/faxes.txt (Mac/Linux) or C:\Documents and Settings\John\Documents\faxes.txt (Windows). You can "cd" to a relative path or an absolute path. If you ever want to go up a level, type

cd ..

Next commands

"ls" and "cd" are the two most essential commands; spend a little bit of time getting comfortable with navigating a directory structure quickly. Do some exploring on your system. To see the contents of a file, type "less [filename]", where [filename] is the name of the file you want to see. To quit out of the program "less", just type "q".

The next step is really up to you. Here are some fun possibilities (not available on all computers):

  • "who" will tell you who else is logged into this system.
  • "fortune" will spit out some wisdom
  • "/sbin/ifconfig" will tell you about the network card (the command is "ipconfig" on Windows)
  • "top" will show you how much CPU different processes are eating up
  • "date" will tell you the time
  • "cal" will display this month's calendar

Now use "cd" and "ls" to examine the contents of "/bin" and "/usr/bin". These directories are where the command line programs are stored. Try running several of these, or if you're scared of doing damage, type "man [command]" first. That will bring up the manual ("man page") of pretty much any command, where you can read about what it does and the options for running it. Read up on a bunch of them, including "ls" and "man". If at any time a command seems to be going out of control (try typing "yes" at the command prompt), just press Ctrl-C and it will quit. If you don't know what something on a man page means, in fact if you're confused about anything at all, search for it on Google. Google is the single most important resource for learning how to use and manage a computer from the command line.

One of the most powerful aspects of the command line is the shell script (known as the batch file on Windows), which lets you put a bunch of commands into a file and run them all at once. This can be especially useful if you want to run some commands every time you log in. Look up "startup scripts" on Windows, and the "bashrc" on Mac/Linux. Also, it might be time you learned Emacs or Vim. Anyway, where you go from here depends on your interests. Happy exploring!

Also see Wikipedia's article on command line.