Monday, October 6, 2008

Useful and Essential Commands

Essential Commands

dirLists names of files in current directory (folder)
dir Hello.*Lists all files whose names start with Hello.
cd C:\filesChanges directory to C:\files ("absolute" pathname)
cd myfilesChanges directory to myfiles subdirectory of current directory ("relative" pathname)
cd ..Changes directory to "parent" of current directory
notepad Hello.javaMS-DOS text editor, used to create and edit ASCII textfiles
type Hello.javaDisplays contents of ASCII textfile on screen
exitEnds command interpreter, makes console window go away...

Useful File Management Commands

copy Test1.java Test2.javaCopies contents of Test1.java to new file Test2.java
ren Test1.java Test2.javaRenames Test1.java to Test2.java
del Test1.javaDeletes Test1.java (Be careful with this command...)
del Test1.*Deletes all files with Test1 prefix (Be even more careful with this command...)
mkdir playpenCreates new directory playpen as subdirectory of current directory
rmdir playpenRemoves directory playpen (must be empty)

DOS File Manipulation

MS-DOS users

Below are steps on how to copy a single file from one directory to another directory as well as how to copy multiple files from one directory to another directory.

Copying a single file from one location to another.

  1. Using the cd command, move to the directory that contains the file you wish to copy.
  2. Type a command similar to the below command.

    copy myfile.txt c:\my\location

    In the above example, you would substitute "myfile.txt" with the name of the file you wish to copy, and "c:\my\location" with the directory you're copying to.

Copying multiple files to another location

  1. Using the cd command, move to the directory that contains the files you wish to copy.
  2. Once in the directory that contains the files you wish to copy, type a command similar to one of the below commands.

    copy *.* c:\mydir

    In the above example, the command would copy every file in the current directory to the "mydir" directory.

    copy *.txt c:\mydir

    In the above example, the command would copy every txt, or text file, in the current directory into the "mydir" directory.

Additional examples of wildcard characters can be found on our wildcard dictionary definition.

See our cd commanddir command, and/or our copy command pages for additional information about each of these MS-DOS commands.

DOS and Its Directories

DOS uses directories to organize the files on your disks. That means we need to use directory commands to create a structure to store our files, and to find the files we have stored there. The commands we need are relatively few:

CommandPurpose
MD 
(or MKDIR)
Create a new directory or subdirectory
RD 
(or RMDIR)
Remove (or delete) a directory or subdirectory
CD 
(or CHDIR)
Change from the current working directory to another directory
DELTREEErases a directory, including any files or subdirectories it may contain.
DIRList the contents of the current working directory

Because working with directories is central to what DOS does, all of these (except DELTREE) are Internal commands, contained within COMMAND.COM, and therefore loaded into RAM and ready for your use whenever you boot, including from a boot disk. You will note that the first three commands have two versions, a two-letter shorter name and a longer name. There is no real difference in use, so I will use the short form consistently in the presentation.

MD

This command creates a new directory or subdirectory. (Actually, since the root is the main directory, all directories are subdirectories. So I will refer to subdirectories in all of the following.) Optional argument is the PATH, but if no PATH is included, the subdirectory will be created in the current working subdirectory

Example:

C:\>md letters

This would create the subdirectory C:\letters

With a path included, you can create a subdirectory anywhere.

C:\>md c:\letters\love

If you are in a different working subdirectory:

C:\letters\>md love

This would have the same effect as the previous example. Since we were already in the C:\letters subdirectory as our current working subdirectory, we can leave out the path information since this is where we want the subdirectory created.

Limitations: The length of a PATH specification cannot exceed 63 characters, including backslashes.

RD

This command removes a subdirectory. The subdirectory must be empty. If it contains files and/or subdirectories, you will get an error message. This also has an optional PATH argument, and has the same syntax as MD. Note that you cannot remove the current working subdirectory. To do this, CD to the parent subdirectory first, then remove the undesired subdirectory.

The RD command can sometimes be a little confusing because of the safeguards that DOS builds into the command. The idea that you cannot delete a subdirectory that has contents, for instance, is a safety measure. (DELTREE gets around this, but is a dangerous command for precisely that reason.) How can you tell if a subdirectory is empty? By using the DIR command to display its contents.

CD

This command changes the current working subdirectory to another subdirectory. Imagine a computer with the following directory structure:

c:\
   \letters\
              \love\
              \business\
   \memos\
              \school\
              \Internet\

Right now you are in the working subdirectory C:\letters\love\. If you want to change to c:\letters\business\, you need to specify the path:

c:\letters\love\>cd c:\letters\business

If, however, you were in the working directory C:\letters\, you would not need to use the path since the default is to go "downwards":

c:\letters\> cd business

You can also use shortcuts:

cd \ will take you back to the root directory from wherever you are.

cd .. will take you the parent subdirectory of the current working subdirectory. So in the first example, you could also go in a two-step process:

c:\letters\love\>cd ..

c:\letters\> cd business

cd . will not do anything, but it is a valid command. The single dot means the current working subdirectory, which is where you already are.

DELTREE

This command was added later as an external command. It will delete an entire subdirectory "tree", i.e. a subdirectory, plus all of the files it contains, plus all of the subdirectories it contains, and all of the files they contain, etc., all in one easy command. This makes it a dangerous command, because it can wipe out so much stuff so easily. It even ignores file attributes, so you can wipe out hidden, read-only, and system files without knowing it. You can even wipe out multiple trees by specifying them in the command:

C:\>deltree c:\letters c:\memos

This would wipe out both of these subdirectories in one command.

This is one of those commands where you really ought to think twice before you use it. It has its place, definitely. I can reme,ber how tedious it was to first go into each subdirectory, delete the individual files, check each subdirectory for contents, delete each subdirectory one at a time, then jump up one level and repeat the process. DELTREE is a great timesaver when you need it. But I would never use it for ordinary maintenance because one false move can do so much damage.

DIR

This command display the contents of a subdirectory, but it also can function like a search command, which many people do not realize. This is one of the most used commands in all of DOS, and learning to use it properly is a great time saver.

DIR will display the contents of the current working subdirectory, or with an optional PATH argument it will display the contents of some other subdirectory. The real power of the DIR command comes with all of the optional switches available to you. You can display files in a variety of formats, for instance:

SwitchPurpose
/pWill pause the screen when a full screen's worth of information has been displayed. You will see "Press any key to continue...", and press of a key will display one more screen. Great for searching those long lists.
/wWill display the file names and subdirectory names in several columns (i.e. wide), but without any other details.
/bBare format, displays file names only, without any other information.