The Command Line

Before the invention of the mouse, the command line was the only interface folks had to navigate through the directories on their computer and launch programs (among other things). A mouse and a graphical user interface (a.k.a., a "GUI") makes doing such things more intuitive, but the command line console still has its own set of advantages: it's simple (i.e., it has a low overhead in terms of memory and processing power needed to run); it's flexible, it's fast, and it's very powerful (i.e., you can accomplish a lot with just a few typed commands).

Gaining Access to the Command Line

In Windows 7, there are two different versions of the command line that you can use -- with one being definitely better than the other. The old MS-DOS based command line can be started by going to the start menu, typing command in the run/search text field, and hitting return. This, however, is a legacy application and rife with shortcomings. Microsoft realized this, and in 2006 released Windows Powershell, a unix-inspired command line application with much greater functionality. To launch powershell, again go to the start menu, but this time type powershell and hit return.

Once launched, you should see something like the following:



In OS X+ on a Mac, to gain access to the command line you will want to launch the "Terminal" application. There are a couple of ways to do this -- one is to simply hold down the ⌘-key and hit the space bar to open Spotlight, and then type "terminal" and hit return. The result should look something like the following:

If you start typing something, the text typed will advance the flashing cursor position symbol (as marked by the small white or grey-filled rectangle above). To the left of the initial cursor position is some text called the prompt. As can be seen in the above images, the prompt can look different depending on your operating system, your current directory, the "name" of your computer, and so on... By default, however, the prompt will end with "$" under Mac OS X+ and with ">" under Windows 7 Powershell. In case you are curious, Unix machines also use the "$" symbol by default at the end of their prompt.

The command line prompts used in each of these environments can be customized, if desired. If this interests you, a simple google search on the subject should get you started -- but we won't go into that here, lest our discussion drift away from the absolute bare essentials of command line access and basic navigation. For simplicity's sake, the remainder of the examples shown on this page, will use a "$" with no preceding text as the prompt...

Printing the Working Directory (pwd)

Suppose you are browsing through the folders/directories of your computer by opening and closing various windows. One of those windows will be the active window (often it's border is shown in color), while the rest of the windows will be sitting in the background (often, their border has been slightly "greyed out"). If you perform any actions (for example -- copying or pasting files), you know that the results are typically tied to the aforementioned active window. To do anything with regard to the other windows, one has to make them active first (typically, by simply clicking on them somewhere). The command line works in a similar way. One directory, called the working directory can be easily affected with commands typed into the command line, while others require some additional work first. To find out what your current working directory is, simply type at the prompt pwd (which is short for "print working directory"), and you should see something similar to the below.

$ pwd
/Users/joe_user
This means that you are currently working in the "joe_user" directory, which is itself inside the "Users" directory. If you are on a Windows machine, you may notice this "path" to the working directory gets printed in a slightly different way. The above path, for example, might show up as C:\Users\joe_user. Two things should draw your attention. First, in Windows a path to a directory starts with a drive letter followed by a colon (i.e., C:), indicating on which drive the directory can be found. Second, the slashes that separate the directory names are reversed. Microsoft has always used the backslash "\", while Macs (and Unix) prefer the forward slash "/". There are no right or wrongs here -- just different systems behaving slightly differently.

Listing the Contents of a Directory (ls)

You can see what files and directories are in a directory with the ls command. It's simplest usage -- typing ls at the command prompt and hitting return will produce a list of all files and directories in the working directory, in alphabetical order. Windows shows these names together with information regarding creation dates and times, file sizes, etc... while in Mac OS-X+, one needs to type ls -l to get more than just the names of the files and directories (note, the extra "l" stands for "long form"), as can be seen in the example below:

Mac OS-X+:
$ ls -l
total 1296
-rw-r--r--@  1 oser  staff   28002 Aug 12 13:06 oxford_logo.gif
-rw-r--r--@  1 oser  staff   11747 Aug 12 13:08 math_center_banner.gif
-rw-r--r--@  1 oser  staff  158840 Aug 12 13:39 mandlebrot_set.jpg
drwxr-xr-x  50 oser  staff    1700 Jul  3 19:15 java_programs
-rw-r--r--   1 oser  staff      16 Aug 13 15:16 test.txt

Windows:
$ ls
    Directory: C:\Users\oser\Desktop\

Mode                LastWriteTime       Length Name
----                -------------       ------ ----
d----         8/13/2013   3:35 PM              java_programs
-a---         4/29/2013   8:11 AM       158840 mandlebrot.gif
-a---         4/29/2013   8:13 AM       11747  math_center_banner.gif
-a---         4/29/2013   7:17 AM       28002  oxford_logo.gif
-a---         4/29/2013   6:11 AM       16     test.txt

Note, in both cases above, the current working directory has another directory inside of it called java_programs. We know this sub-directory is a directory because of the little leading "d" at the beginning of the corresponding row.

Changing Directories (cd)

You can change your working directory with the cd command. For example, assuming that your current working directory has three directories inside of it, named "college", "home", and "work". We can make the change the working directory to "college" with cd college as the below shows:

$ pwd
/Users/joe_user
$ cd college
$ pwd
/Users/joe_user/college

If you need to change the working directory to the directory that encloses the current directory (which is called the working directory's parent directory), you can refer to that directory with two periods typed in succession (i.e., ..). So, to continue our example from above -- if we wished to change the current working directory from college back to joe_user, we might do the following:

$ pwd
/Users/joe_user/college
$ cd ..
$ pwd
/Users/joe_user

To get to an even "higher" directory, we simply use the .. symbol more than once, as shown here:

$ pwd
/Users/joe_user/college
$ cd ../../
$ pwd
/Users

You can also change directories by providing a path to the directory in question. This directory can be either absolute or relative. To see the difference between the two, suppose the directory college used in the example above had another directory in it called cs170, and our current working directory was /Users/joe_user. To change our working directory to cs170, we might do the following:

$ pwd
/Users/joe_user
$ cd /Users/joe_user/college/cs170
$ pwd
/Users/joe_user/college/cs170

The above is an example of using an absolute path to a directory. Often however, we can often be more efficient (i.e., type less) by using a relative path to the directory (or file) in question, like the below demonstrates:

$ pwd
/Users/joe_user
$ cd college/cs170
$ pwd
/Users/joe_user/college/cs170


More Commands...


The cat command

This command allows one to print to the screen the contents of a text file.

Example:


The cp command

This command allows one to make a copy of a file or directory

Examples:


The mv command

This command allows one to rename and/or relocate a file.

Examples:


The rm command

This command allows one to remove an existing file.

Example:


The mkdir command

This command allows one to make a new directory of a given name.

Examples:


The rmdir command

This command allows one to remove an existing directory.

Example:


The man command

This command allows you to learn about any other command. The metaphor here would be looking up the command in question in the "manual". Invoking man will bring up text detailing the command in question, citing what it does, how it and it's arguments should be called, what happens when optional flags are provided, and so on...

Example:


The nano editor

nano is a simple text editing program that will run in a terminal/console window. Note, in such a program, one obviously doesn't have the benefit of a mouse. Thus, moving the cursor around must be done with the arrow keys and other keyboard shortcuts. Copying and pasting, searching for specified text, indenting, and other operations are all handled in this way -- through the use of keyboard shortcuts.

Note: The nano editor does not come as a standard part of the windows operating system, so to use it in this environment you must install it first. (See nano for windows)

Example


The vim editor

vim is a much more powerful text editing program that also runs in a terminal/console window. The learning curve, however, is substantially steeper. If you want to learn how to use it, this interactive tutorial is a nice gentle way to start...

Note: The vim editor does not come as a standard part of the windows operating system, so to use it in this environment you must install it first. (See vim for windows)

Example