Using the MS-DOS Command Prompt

30 November -0001
by: Justin C. Klein Keane
Last update March 21, 2006

With the emergence of Windows XP the MS-DOS command prompt is increasingly being buried beneath the graphical user interface that has dominated all Windows distributions. However, this trusty utility has never been more useful for me. Knowing how to use the command prompt is essential in order to do any power computing. Hidden in the 'Accessories' folder of the Program Files, the 'Command Prompt' actually points to cmd32.exe, found in %SystemRoot%\system32\cmd.exe.

Expanding the Start menu and locating the 'Command Prompt' under Programs you can right click it's icon and view the default properties. You may notice that you can alter at what location the command prompt begins in the directory tree. By default it starts in the users' home directory, but often it is more useful to have the command prompt start at the root of one of your drives (C: or D:). You can modify the 'Start in:' option to change this. You'll also notice other tabs in the properties window, such as options, font, layout, and colors. These allow you to change the appearance of the command prompt. I recommend using the 'QuickEdit mode' and 'Insert Mode' (found in the Edit Options frame on the Options tab) since those modes allow you to cut and paste out of the command window.

Starting with Windows 2000, the Command Prompt began remembering command history, which is nice. It also supports command history. What this means is that you can press the up arrow and scroll through previous commands that you've issued. Completion means you can type a portion of a directory name or file name and hit the 'Tab' key and the prompt will attempt to complete the directory or file name.

Navigation

Navigating the command prompt is a little discouraging for beginners. There is no intuitive way to determine what commands can be used to change directories, list directory contents, or even print out the current directory that the command prompt is working in. Lets begin with figuring out where you are. To determine the current working directory in MS-DOS type:

cd

This stands for 'current directory' and it should prompt you with the current working directory. You'll probably see a long list of directories that start with the drive letter then a list of directories, such as:

D:\Documents and Settings\jukeane

To change directories you can use the cd command again. Two periods means 'up one directory', so if you want to move up a directory type:

cd ..

You can combine these commands, so if you wanted to go up on directory, then descend into another directory named 'files' you could use:

cd ..\files

To change drive you only have to type the drive letter and a colon. So for instance, if we want to change to the C drive just type:

c:

Sometimes you want to change into a directory with a space in the name. This is tough because cd expects that the next string you type after the command will be the directory name, and if there is a space then the next word will be interpreted as an alternative command. So, if you want to change to the 'My Documents' folder you have to quote the name of the directory like so:

cd "My Documents"

dir

To read the contents of the current working directory you can use the 'dir' command. This will list all of the files and folders that are in the directory. Sometimes this list gets so long that all the contents scroll past the screen before you can read them. In these situations it's handy to use a flag, or optional argument, to alter the behavior of the command. By using the command:

dir /P

the listing will pause at the end of each screenful of information. You can use the wildcard '*' symbol to search for specific things when doing a directory listing. For instance, using:

dir *.txt

will locate all of the files with the '.txt' extension in the current directory.

help

If you ever need more information about how to use a command simply type 'help' then the command name. For instance:

help dir

will bring up the help information, including usage and optional flags that can be used with the dir command.

move

You can move files around using the move command. For instance, if you want to move a file called 'foo.txt' into a subdirectory called 'temp' you could use the command:

move foo.txt temp

You can use wildcards with move as well, so you could move all the files with a certain extension to a specific directory.

copy

If you just want to copy a file to a specific location you can use the copy command in the exact same way as the move command. So for instance, using the above example if you wanted to change directories into the temp directory, move foo.txt back to it's original directory, then copy it back into the temp directory you could use:

cd temp
move foo.txt ..
copy ..\foo.txt .

You'll notice that the single period refers to 'the current working directory'.

Redirecting

You can redirect the output of a command at the command prompt by using either the greater than or double greater than signs. Using greater than moves the output of the first command into the file specified after the greater than sign. Using the single greater than sign will overwrite anything existing in the file, while using the double greater than sign will append the output to the end of the file. So for instance:

echo foo > sample.txt

Will create a file sample.txt with a single line that reads 'foo'. If you then use:

echo text > sample.txt

The file sample.txt will now contain one line that reads 'text'. You can use the symbols \n for new line and \r for carriage return so you don't jumble things up when echoing them into files. This technique is especially useful if you want to create a text file out of the output of a command. For instance, the 'netstat' command will list the status of all network connections on the computer. You could use the command:

netstat > netstat.txt

To create a text file with the output of the netstat command.

Edit

Edit is a handy text editor that works inside the command prompt. The trick to using edit is to understand that it is a keyboard only text editor. You have to use the 'Alt' key to access any of the menus in edit. For instance, if you edit a file and want to save it you have to press 'Alt+f' then use the arrow or tab keys to move among the file menu to select save. This can be a little disconcerting for many GUI users.

Aliasing

Sometimes you use a command over and over and you want to find an easier way to use that command. In the Unix world there is a concept of aliasing. This doesn't translate directly to DOS, but it can be done. For instance, I'm used to working in BASH, a shell similar to DOS that is used on Unix and Linux machines. BASH uses the command 'ls' to list directory contents. This means that when I use the command prompt I often end up typing 'ls' to do a directory listing rather than using 'dir'. To solve this problem I created a new batch file called 'ls.bat' and put it in the %SystemRoot%\system32\ folder. This is the default folder that the command prompt looks in when trying to locate programs that you invoke at the command prompt (for instance, if you look in that folder you'll find the program 'netstat.ext'). To create this new batch file I change directories to the system32 folder using:

cd %SystemRoot%\system32

Then type 'edit ls.bat' and enter the following into edit:

@ECHO OFF
dir /P

Next type 'Alt-f' and key down to the save menu, then type 'Alt-f' then key down to 'exit'. Now you can test out your new aliased command. Change to a different directory and type 'ls' and see how the new command works.

If you want to get really fancy and create lots of aliases, but keep them all in one place you can create a series of batch files in a common directory that is easy for you to find (such as 'C:\aliases'). Then you can add this directory to your 'Environmental Variables' so that they're always available. To do this you have to right click on the My Computer icon, select the 'Advanced' tab, click the 'Environmental Variables' button, then add a new environmental variable called 'PATH' with the value 'C:\aliases'. You may already have a PATH variable, in which case you can append the new directory to whatever is already listed there by adding a semi-colon then the new directory path.

variables

Variables are very useful in the command prompt. Variables are delimited by the percent sign. Many variables are 'Environmental' and are set up when you log into the machine. For instance, if you type:

echo %HOME%

You can see what the %HOME% variable is currently set to. You can even set up your own variables using:

SET FOO='hello world'

Then you can see that variable by typing:

echo %FOO%

What you've just done is basic to creating batch files. Batch files are groupings of DOS commands that run like a linear program. Batch files are incredibly useful for system maintenance and automating tasks. For instance, the following batch file can be used to start up a number of programs with one click:

@ECHO OFF

REM This just fires up all the programs I need during the day (so I don't forget any)

start C:\"Program Files"\"Mozilla Firefox"\firefox
start C:\"Program Files"\"Mozilla Thunderbird"\thunderbird

echo %DATE% %TIME% %USERNAME% >> startup.log

This program will start Mozilla firefox and thunderbird, then write the date, time, and user who called the batch file to a text file called startup.log. You can create batch files in any editor, even by using the 'edit' command from the command prompt. ECHO simply turns off program output to the screen and REM is a remark (the batch file will ignore that line).

Customizing the Prompt

If you're like me you hate to see the huge listing of your current working directory when you're using the Command Prompt. In some cases it can take up most of the screen real estate. The easiest way to change this is to create a new environmental variable (right click on 'My Computer' -> Properties -> Advanced tab -> Environmental Variables button -> click 'New' under user variables). Set the variable value to $d for the date, or $t for the time. To see a full list of options type in 'PROMPT/?' at the command line. You can string multiples together as well as using other variables, such as %USERNAME%.