Using the DOS Command-line

Today, most of us comunicate with our computers by using a Graphic User Interface (GUI) such as Microsoft Windows, instead of by using a Command Line Interface (CLI). As a flegeling programmer, however, you'll find that there are some things that are difficult to do from a GUI. In this lesson, you'll get a quick introduction to DOS, the CLI that underlies MS Windows. The goal will be to teach you enough to get through the semester, not to make you into a master of DOS aracana. 
 


The Working Directory

DOS commands are typed into a DOS window, which you can launch by clicking Start, clicking Programs, and then clicking MS DOS Prompt as shown here:
Selecting the MS-Dos Prompt shortcut from the Start Menu.
If you are using Windows ME or Windows 2000, the MS DOS prompt has been renamed as the Command Window, which is accessed via the Accessories menu.

When the DOS window first appears, it shows the name of the current, or working directory (folder). The name of the directory includes the drive letter, followed by a colon, followed by a backslash and a directory name, followed by a prompt symbol (>) like this:

An MS DOS window after it is first opened.
In this example, the current directory is the WINDOWS directory of your C: drive. If you see a longer directory name like:
 
C:\WINDOWS\SYSTEM>

it means that the current directory is the SYSTEM directory, which is a subdirectory of the WINDOWS directory stored on the C: drive. A series of directories like this, joined together using backslash characters, is known as a path. (This is just like the path used in a URL, except that URL paths follow the UNIX convention of a forward slash instead of a backslash.)


Changing the Working Directory

You move from one directory to the other by using the cd command, which can also be written chdir. If your working directory is C:\WINDOWS, you can move to the root [highest-level] directory by typing cd \ like this:
Changing to the root directory using the cd command.
You an type the command in either upper or lower case. When you press ENTER, DOS will carry out the command, and give you a new prompt that shows you have successfully changed to a different working directory.

Here's another example. Suppose you want to move to the C:\WINDOWS\SYSTEM directory. You simply type the full path name of the desired destination:

Changing to the WINDOWS\SYSTEM directory using the CD command.
When you're in a subdirectory, you can move to the directory that holds it (called the parent directory) by using two dots for the directory name like this:
 
cd ..

For example, if you are in the C:\WINDOWS\SYSTEM directory typing this command will take you to the C:\WINDOWS directory. 

If you want to move to a subdirectory of your current directory (a child directory, in other words), you can just type the directory name, rather than the complete path. If you are in the C:\WINDOWS directory and you want to move to the C:\WINDOWS\SYSTEM directory. You can simply type 
 

cd SYSTEM

rather than
 

cd C:\WINDOWS\SYSTEM

In Windows, many folder names, such as "My Documents" and "Program Files" contain spaces, unlike the original version of DOS, where file and folder names were limited to eight characters and a three-character extension. To handle a pathname that contains spaces, put quotes around the name, like this:
 

cd "C:\My Documents\MyWebPages\"

To change from one drive to another, you don't use the cd command. Instead, you just type the name of the drive followed by a colon like this:

Changing from the C: drive to the D: drive.


Directory Listings

Once you've navigated to the directory you intend to use, you can use the dir command to see the names of the files (and subdirectories) that it contains. Here is a directory listing from one of the directories on my C: drive.
A DOS directory listing.
The listing shows contents of the current directory. Each file or subdirectory is shown on a separate line. Each entry actually has two names. The first name [the left-hand column] is the short "DOS" file name. This name is created by Windows; for our purposes you can just ignore it. The far right column contains the file's "real" name; note that the real name can contain both upper and lowercase characters, and does not have a limit to its length.

There are some other parts of the directory listing that you might find of interest:

  • The second column contains the word <DIR> if the entry is a subdirectory, and the size of the file in bytes if the entry is a regular file. 

  •  
  • The first two entries, each of which is a subdirectory, have rather peculiar names. You can ignore these entries if you like: They refer to the current directory (.) and its parent directory (..). 

  •  
  • The last two lines are a summary of the files in the directory. The of these shows that 18 files are listed (subdirectories and the two special entries are counted as files) and the files occupy a total of 49,057 bytes. The final line shows that over 1,843 MB of free space is available on the drive.
You can list the contents of a directory other than the current directory by specifying the name of the directory as an argument. For example, the command
 
dir C:\WINDOWS

lists the contents of the C:\WINDOWS directory. If you attempt this, you'll find that there are too many lines to fit in the DOS window, and so the files will scroll off the top of the window. To prevent this, add the /p flag to the end of the dir command:
 

dir C:\WINDOWS /p

This causes the listing to stop each time the window fills up. You can read the listing and then press Enter to view the next page.


Deleting, Copying, and Moving Files

You can remove unwanted files by using the del command, which can also be written erase. For example, to delete the README.TXT file, just type
 
del README.TXT

When using the command-line, deleted files are not placed in the "Recycle Bin", and there is no friendly prompt asking "Are you sure?": the file is instantly and silently deleted from your drive.


WildCards

Sometimes, you'll want to delete several files at once: all of the .class or .tmp files in a directory, for instance. To do this, the delete command allows you to use the asterisk (*) as a wildcard character. You can delete all the files in the current directory with a file extension of .class by typing
 
del *.class

In the same way, you can delete all the files in the current directory by simply typing
 

del *.*

Obviously, this is very powerful, so be extra-careful when you use wildcards. If you accidentally type del *.java, when you meant to type del *.class, all of your .java files in the current directory will be erased; you will not be able to get them back.


Copying Files

You can make a copy of an existing file by using the copy command. For example, to copy the file index.html to the C:\TEMP directory, just type
 
copy index.html C:\TEMP\

Just like with del, you can use wildcards with the copy command. To copy all the HTML files the current directory to your A: drive, you would type
 

copy *.html A:\

You can use the copy command to rename or move a file. To rename MyApplet.java to MyFirstApplet.java, you could type the following two commands:
 

copy MyApplet.java MyFirstApplet.java
del MyApplet.java

This is relatively inefficient, however, since it takes time to copy all the bytes and there must be room on the drive for both the original file and the new file. A better approach is to use the ren command, which can also be written rename. For example, you could accomplish the same thing as the previous example by typing
 

ren MyApplet.java MyFirstApplet.java

Because the ren command doesn't actually move the bytes of the file, both the original file and the new file must be in the same  directory.


Creating and Removing Directories

The final DOS skill you'll find useful is the ability to create and remove directories. To create a directory, you use the md command, which can also be written mkdir. For example, to create a subdirectory named MyJavaFiles, you type
 
md MyJavaFiles

The directory is created as a subdirectory of the current directory. If you like, you can create the directory as a subdirectory elsewhere by typing the full path name of the directory you want to create. For example, to create a directory named MyJavaFiles that is a subdirectory of C:\My Documents, you can type
 

md "C:\My Documents\MyJavaFiles"

Notice that we've used quotes around the directory name because the parent directory, (My Documents) has a space in it. In Windows 95 and 98, the parent directory must already exist. Windows NT and 2K will create the necessary parent directories.

To delete a directory, you use the rd command, which can also be written rmdir. The directory must be empty; that is, it must not contain any files or subdirectories. It must also not be the current working directory of any DOS window or running program. To delete a directory named C:\MUD, type
 

rd C:\MUD

The directory is instantly and silently deleted. If you're in a hurry, you can delete a directory and all the files and subdirectories it contains by using the /s flag. Typing
 

rd C:\MUD /s

will delete the C:\MUD directory and all it contains. But, DOS will politely ask you if you're sure before completing the irreversible deletion.


Running Programs

DOS programs are run by typing the program name, and any arguments you want to pass to the program, from the command prompt. [You can also start Windows programs this way.] To start the MS-DOS editor program, for instance, you would type in the command edit like this, and then press ENTER:
 
C:\MyFiles> edit

When the Edit program starts up, it will use the entire MS-DOS window like this:

Running the EDIT program in an MS-DOS window.
Notice that the title-bar of the DOS window changes to let you know you are running the EDIT program. When you exit the program, you'll be placed back at the plain MS-DOS prompt. With MS-DOS program, you have the option of running the program in a window, as shown here, or full-screen. You can switch between the two modes by holding the ALT key down while pressing ENTER.

When you start a Windows program from a DOS window, it does not run "inside" the DOS window, but launches it's own Window. To use Windows Notepad to create a C++ file called MyProgram.cpp, you'd simply type Notepad MyProgram.cpp and press ENTER like this:

Starting Notepad from a DOS window.
As soon as Notepad starts up, you'll get a new DOS prompt, and you can switch between the Notepad window and the DOS window by using the mouse.


Materials on this web site © 1995-2000, Stephen Gilbert. All rights reserved.
Any materials may be reproduced for non-profit purposes so long as
(1) this notice remains intact and (2) you notify me of your use.