Operating Systems

Command Line User Interface

[ Unix (bash) vs. Windows (CMD.EXE) ] [ Back to Lecture Notes ]


How to Get to a Command Prompt (and back)

Use either of the following methods to open a Windows Command Prompt session:

RUN Command Command Prompt Shortcut
  • Open a Command Prompt window using the RUN command:  Start > Run
  • Click OK
  • Click on the Command Prompt menu item on the Windows Start menu or one of its sub-menus.
    • By default a Command Prompt shortcut will be in the Accessories sub-menu tree:
      Start > Programs > Accessories Figure 2

    • The Command Prompt shortcut may also be in the Start menu main opening menu


At the Command Prompt

A Command Line Interface is usually a prompt consisting of some character or series of characters and a blinking cursor. The typical DOS prompt is C:\> and is frequently referred to as the "C-Prompt." C: is the disk drive letter; if you were at a command prompt from the E: drive it would be E:\>. The \ character is the start of the path; most systems are setup to display the current working directory as part of the prompt. If you were in your Lab4 folder within your Cis120 folder on the E: drive, for example, the prompt would be: E:\Cis120\Lab4>.

Basic Navigation

All commands in a Command Prompt session must be typed on the command line followed by pressing the ENTER key. That is why it is called a "command line interface" :-)

Depending on the method you used to open the Command Prompt window, and depending on how the system is set up, you will probably start at the root of the C: drive, the Desktop folder, or possibly a Windows operating system sub-directory. With the possible exception of the Desktop, you more than likely want to be in a working directory dedicated to a specific project or assignment such as A:\cis121\project, or D:\wr121\essay3 .

Navigating Between Drives and Directories

  • The Command prompt (text displayed by the system, not what you type) is shown in blue:   C:\>
  • Examples are displayed in a monospace font so that the space character is easy to see.
    Spaces display with a black border.
  • All commands are terminated by pressing the Enter key (possibly labeled Return or with a left-facing arrow: enter on some keyboards).
Action Key Strokes Example Windows Explorer View
Go to different drive drive letter, colon, ENTER
C:\>A:

A:>
A:
Go to a directory immediately below the root CD, space, back-slash, name of directory, ENTER
A:\>CD \cis120

A:\cis120>
CD cis120
Go to a directory immediately below current working directory CD, space, name of directory, ENTER
A:\cis120> CD lab4

A:\cis120\lab4>
CD cis120
Go directly to root of current drive CD, space, backslash, ENTER
A:\cis120\lab4> CD \

A:>
A:
Go directly to a sub-directory in one command CD, space, full name of directory, ENTER
A:> CD \cis120\lab3

A:\cis120\lab3>
CD cis120
Go to "parent" directory of current working directory CD, space, period, period ENTER
A:\cis120\lab3> CD ..

A:\cis120>
CD cis120

Command line programs typically have options (also called "switches") as well as arguments or parameters that can follow the command. (Technically commands have parameters; users pass arguments on the command line.) In all cases you must leave at least one blank space between the command name and its options and/or arguments. When you have completed typing the entire command and options you then have to press the ENTER key to signal the operating system to carry out the command. Most DOS commands have, at the very least, a brief command syntax help by typing the command name followed by a space, and the /? characters. The / character is the character to signify that the next character(s) is an option.


Basic DOS Commands

  • At least one space is required to separate commands, options, and arguments.
    (shown here with a black border as a reminder:  )
  • All commands must be terminated by pressing the Enter key.
Command Description Example Comments
d: Go to Drive d
d is the drive letter you want to go to
See Navigating Between Drives and Directories above
CD Change Directory
Follow CD with a space and the path you want to change to.
DIR Directory Listing of files
DIR
List all files in current directory
DIR *.HTM
List all files with HTM extension
DIR /S *.TXT
List all text files in current directory and all directories below current directory
CLS Clear the Screen
CLS
 
cmd /? Get help for command specified in cmd
COPY /?
Help for COPY
COPY Copy files
COPY index.htm A:
Copy index.htm in current directory to floppy disk
DEL Delete files
DEL temp.txt
Delete temp.txt; does not get saved in Recycle Bin
REN Rename
REN lab5.html lab5.htm
Rename lab5.html to lab5.htm
SORT Sort a text file
SORT names.txt
Sorts the lines of names.htm
SORT /R /+18 filelist.txt
Sort filelist.txt in descending order starting at column 18 in each line.
FIND Find text in a file
FIND "<IMG" index.htm
Find (and display) all lines in index.htm that contain the HTML IMG tag.
FINDSTR Find text using regular expressions (WinNT/2K/XP only)
FINDSTR "<[/]*[OU]L>" *.html
Find (and display) all lines in all HTML files in the current working directory that contain either the Ordered List tag or the Unordered List tag, and include the beginning as well as the ending tags.

Piping and Redirection

Command Description Example Comments
cmd1 | cmd2 Pipe output from one command to input of next command
(vertical bar, the "upper case" character on the back-slash key, typically just to the right of "=" and "+")
XCOPY /? | MORE
Help for XCOPY (which is longer than one screen); "pipe" the output from XCOPY to the input MORE, which displays one screen of output at a time.
TYPE index.html | MORE
Display the contents of index.html, one screen of text at a time.
cmd > file Redirect output from a command to a text file.
If the file already exists, the old file is overwritten with the new contents; otherwise creates a new file.
DIR >myfiles.txt
Creates a text file named myfiles.txt in the current directory with the output of the DIR command.
If there was a file named myfiles.txt in the current directory, its contents are overwritten and a new file is created.
cmd >> file Redirect output from a command to a text file.
Appends to the end of a file if it already exists; otherwise creates a new file.
DIR *.gif >>myfiles.txt
Get a directory listing of all GIF files in the current directory and append the output to myfiles.txt.

The commands listed and discussed above are meant as a "survival guide" to accomplish basic tasks at the Command Prompt. Various sources exist for additional information; see the Command Line Help section below for sources of additional information.

See [ Figure 3 ] for an example Command Prompt session where some of the above example commands are used, plus FINDSTR regular expressions (Win2K/XP only), to find all the HTML files that contain an anchor element referring to the edu domain.

FINDSTR supports a peculiar and very limited subset of regular expressions. See egrep on the Resources > Utilities page for a more "standard" regular expression implementation.


Command Recall & Command Editing

Command recall is available by default in Windows 2000 and Windows XP Command Prompt sessions. In Windows 9x command recall is available via the DOSKEY command: at the beginning of a Command Prompt session simply enter the command DOSKEY. ( DOSKEY also provides command recall in Win2K/XP, you just do not need to remember to enter the command each time).

It is common for commands to be quite long with numerous options and arguments, as well as having long, complicated paths as one or more arguments. For example, take the following command (the system prompt is in reverse video, required spaces are displayed with an "overscore" character, and the Enter key is displayed with a left-facing arrow):

D:\Cis121> xcopy labs\db\*.mdb D:\Cis\DB_Samples\bu\ /D:01-01-2003 /R /I

By the way, that command says "copy all the MS Access database files from the db directory, which is in the labs directory (which is in the current directory) to the directory specified; and copy only those files modified since 01 JAN 2003, overwrite any read-only files if they exist, and create the final destination directory in case it does not already exist." And do it without leaving my current working directory. Whew, what a mouthful!

A few minutes later I slap myself on the forehead as I discover that I really wanted to copy files modified since the beginning of 2002. Knowing my typing abilities, I would not want to place bets on getting that command line perfect the second time! However, all I have to do is repeatedly hit the up arrow key, cycling through previous commands until I see the command. Then I can use the left and right arrow keys to move the cursor to the date argument, change one character, hit the enter key, and execute my revised command.

DOSKEY has many other useful features, such as an option to set the size of the history buffer, and perhaps its main purpose in life, creating "keyboard macros." Enter the command DOSKEY /? for more information.


Putting It All Together

Enter the command TIME and you will see that you are prompted to enter a new time, or just hit the Enter key to accept the current time. But what you really want to do is get the time without the requirement to hit the Enter key so you can redirect the output to a log or documentation file. And while we are at it, let's do the same with DATE.

Note:
Under Win2k/XP the following example can be simplified considerably by appending the /T switch to TIME and DATE. However, many older Win9x systems are still in use, and the example demonstrates the power of building one complex command from several simpler commands.

The VER command displays the operating system version number, but as a side effect it also outputs a newline sequence (the same as pressing the Enter key). We can pipe the output of VER to the input of TIME and DATE so that, in effect, we are sending an Enter key to those commands (the version number information simply gets discarded).

ver | DATE
ver | TIME

Which produces output similar to the following (without the need to hit the Enter key):

    The current date is: Sun 03/23/2003
    Enter the new date: (mm-dd-yy)

    The current time is: 17:15:08.22
    Enter the new time:

So far, so good; but we really don't want to clutter up our log file with "Enter the new ..." lines; we really want a simple time and date header. So now we can use another pipe to send the current output to FIND, looking for the word "current" which only occurs in the line with the date and time:

ver | DATE | FIND "current"
ver | TIME | FIND "current"

Now our output looks like this:

    The current date is: Sun 03/23/2003
    The current time is: 17:20:23.25

Finally, redirect the output of each command to a log file:

ver | DATE | FIND "current" >> logfile.txt
ver | TIME | FIND "current" >> logfile.txt
. . . Do some work on an assignment . . .
DOSKEY /H >> logfile.txt
ver | TIME | FIND "current" >> logfile.txt

Now you have a record of the date and time you started work, a history of all your commands, and the time you finished. As noted above, in Windows 2000 and Windows XP you can simplify the DATE and TIME commands as follows:

DATE /T >> logfile.txt
TIME /T >> logfile.txt

Setting the PATH

When you enter a command on the command line in a Command Prompt window the command interpreter (COMMAND.COM or CMD.EXE) searches for an executable file with the name you typed

  1. First in the current working directory
  2. Then from a special list of directories on your system called the PATH.

You can see the list that is used for the search by entering the command PATH on the command line.

The commands discussed thus far are part of a standard Windows installation and are located in a directory (usually WINNT\system32 or Windows\Command) that is included in the default PATH. However, you may have a command line program that you downloaded for a special purpose, such as unzip32.exe to unzip an archived file because you do not have WinZip (or something similar) installed; or a programming language package of executable files, library files, and supporting dynamic link libraries, such as Perl.

If the program is a single file, and not too big, you could copy it to your working directory each time you need it. However, many applications, such as language compilers, require numerous supporting files in specific directories, and it is necessary to set the PATH to use them.

If you type a command and get a message similar to one of the following, then the executable program file is not in the PATH. (Note that unzip32 is only an example)

    command not found

    'unzip32' is not recognized as an internal or external command,
    operable program or batch file.

Continuing with the unzip32 example, and assuming you will be using it in several directories, download it one time to a known, central location, and set the PATH to that location each time you need in a Command Prompt session, or set the PATH permanently for your user profile. As in the examples shown previously, the system prompt is in reverse video, required spaces are displayed with an "overscore" character, and the Enter key is displayed with a left-facing arrow.

From the Command Prompt:

D:\School\Cis121\DB_Query> PATH %PATH%;D:\School\Downloads

D:\School\Cis121\DB_Query> unzip32 STU_FlyHigh.zip

Changes made to the PATH variable as shown above are only in effect for the current Command Prompt session; you need to issue the command each time you open a new Command Prompt window. This has the advantage that you do not make any permanent changes to system settings. But it also has the disadvantage that you have to repeat the command(s) each time you need them.


Windows 2000 System Properties:

You can set your path permanently in Windows 2000 (the procedures should be similar for Windows XP) for all subsequent sessions in System Properties as follows:

  • Go to System Properties using one of the following:
    1. Start > Settings > Control Panel > System
    2. Right-click My Computer; select Properties
  • Edit the PATH Environment variable:
    1. Select the Advanced tab
    2. Select the Environment Variables... button
    3. Select the PATH environment variable
    4. Select Edit...
    5. Edit the path as applicable
      Note that multiple paths are separated by semicolons, just like at the Command Prompt.
Editing a local user's PATH Environment variable from Windows 2000 System Properties
Figure 4.
Editing a local user's PATH Environment variable from Windows 2000 System Properties

Windows 95/98 AUTOEXEC.BAT:

In Windows 95/98 you need to edit a special start-up file called AUTOEXEC.BAT located in the root directory of the drive where Windows is installed (usually C:\). Add a line to AUTOEXEC.BAT exactly like the Command Prompt PATH setting shown above. In fact, try the command as shown above. If it is correct, create a command history file as discussed above and copy the applicable line to AUTOEXEC.BAT.

If one or more PATH command lines already exist in your AUTOEXEC.BAT then add your new PATH command at the end. Multiple PATH commands, if entered correctly, simply append the new setting to the existing value.

Using the example shown in Windows 2000 System Properties above the same result is obtained in Windows 95/98. You will need to re-boot the computer for the changes to take effect.

@echo off
REM AUTOEXEC.BAT
REM Windows 95 / 98

REM very likely various commands ...
REM  ... don't mess with them!

PATH %PATH%;D:\perl561\bin;D:\j2sdk1.4.1\bin

DOS Text Editor

The DOS text editor EDIT is in many respects a better and more powerful editor than Windows NotePad. The next time you are editing a file in NotePad try to figure out what line number you are on and what column number within that line you are on. This is particularly important when errors are reported by line numbers within a programing language source code file.
See [ Figure 5 ].

From a Command Prompt enter the command EDIT, then activate the menus by pressing the Alt key plus a highlighted "hot key", such as Alt+F to open the File menu. The mouse may or may not work with the menus depending on how the Command Prompt window shortcut has been configured.

You can also include the name of the file you want to open as an argument to the command:

D:\Cis120\WebPage> EDIT index.html

Command Line Help

While "DOS" as an underlying operating system is really finally gone in Windows 2000 and Windows XP, support for the Command Line and Command Line Utilities is significantly enhanced. Navigate to Windows 2000 Help for MS-DOS Commands:

Start | Help > Contents > Reference > MS-DOS Commands

Windows 2000 Command Line Help
Figure 6a Windows 2000 Command Line Help

The HELP command and HELP.HLP file that shipped with MS-DOS versions 5.0 through 6.22 is an excellent source of more detailed DOS command information. 3 files are required:

These files were available on the Windows 95 CD in the \OLDMSDOS directory and are contained in the self-extracting archive file OLDDOS.EXE available from the Microsoft Download site. See the Resources page for a direct link to download OLDDOS.EXE. The 3 files are also available in the file dos6help.zip from this site (533 Kb). One additional file, QBASIC.HLP, is in the zip file, but is not required for the DOS HELP command.

The best way to learn more about DOS and commands available from the Command Prompt is to spend some time with the HELP program and experiment with some commands. Extract the 3 required files from OLDDOS.EXE or dos6help.zip and copy them to a floppy disk (about 485 Kb). Log onto the floppy disk by entering the command A:, then type HELP and press ENTER. Use the TAB key to navigate between topics, then press ENTER to open a help topic. To exit from HELP press the Alt and then the F key to access the File Menu, then press the X key to exit. Finally enter EXIT at the Command Prompt to return to Windows.

HELP program with the File Menu opened and Exit selected
Figure 6b.
The HELP program with the File Menu opened and Exit selected

A good online source for DOS commands beyond MS DOS 6.22 is Using MS-Dos7 Commands at:
http://home7.inet.tele.dk/batfiles/msdos7/


A Word of Caution

Until you know more about DOS commands do not perform any operations on files on your hard disk. Use a floppy disk that does not contain critical files. It is possible to do irrevocable damage with a few short, cryptic key strokes. In particular stay away from the FDISK command, and only use FORMAT on a floppy disk. The general rule of thumb is this:
If you are greeted with a message that

  1. You were not expecting, or
  2. You do not understand

then the answer is almost always NO.


Copy and Paste to and from a DOS Window

One of the more useful features of opening a "DOS Window" from within Windows is that you can copy and paste between Windows applications and DOS applications. You can even copy a command from a Windows application such as MS Word, paste it onto the command line following the prompt, press enter and execute the command.

Lets assume you are working in a word processing document like MS Word, although for this example I will use Windows Wordpad. Lets also assume you are writing some documentation and you want to insert a complete directory listing of the files in your working Lab4 folder into your document.

  1. Open a Command Prompt (DOS) Window (See Getting To a Command Prompt)
     
  2. Navigate to your folder:
    Navigate to folder
    Figure 7.
     
  3. Type DIR and Enter to get a directory listing:
    Get directory listing
    Figure 8.
     
  4. Click on the icon in the upper left corner of the DOS Window, select Edit, then Mark. Pause for a second and notice the other choices in the Edit menu; we will be using the Copy and Paste. Notice also that the Enter key is a shortcut for Copy; that is how we will copy.
    Click on Mark to select text
    Figure 9.
     
  5. Click on Mark, then starting at a corner of the area you want to copy, drag the mouse to highlight a rectangular area to copy.
    Select text with mouse
    Figure 10.
     
  6. Press Enter to copy the selected text to the clipboard, then activate your Windows application. In your Windows application (Wordpad for this example) open the Edit menu and select Paste
    Copy selected text to clipboard using ENTER
    Figure 11.
     
  7. And just like that you have your directory listing in your word processor document!
    DOS Window text copied to Windows application
    Figure 12.
     
  8. You can also copy from Windows applications into DOS applications, or even directly onto the command line.
     

Windows Screen Shots

It is often convenient or necessary to "capture" a screen image for future reference, documentation, trouble-shooting, or to turn in as part of an assignment as verification of successful completion. It is common, for example, in programming classes to capture numerous screen images of program output as part of your lab report.

  1. Make sure the window you want to copy is "in focus," then press Alt + Print Screen (that is, hold down the Alt key while also pressing the Print Screen key. This captures the active window as a bitmap image to the clipboard.
     
  2. Activate your word processor window and Copy the contents of the clipboard into your document.
     

Here is a DEBUG session captured and inserted into a Wordpad document:

DOS Window screen capture
Figure 13.

If you are at home and are cost-conscious, or are in the lab at school and want to make friends with the folks who run the lab, here is a way to save printer ink / tonner. Just select your entire DOS window as if you were going to copy the text as described above, put don't press Enter; you want to keep the entire DOS screen in "reverse video." Now press Alt + Print Screen and copy the graphic image to your word processor document.

Figure 14 shows how the Wordpad document looks after pasting copying the same DEBUG session in a DOS window, only this time I pretended I was going to copy all the text and grabbed the screen shot before pressing the Enter key which resulted in a reverse video DOS screen.

Revised: 05 FEB 2006 21:19