CLI  Command Line Interface  CLI


- Kickin' it Old Skool -

Every programmer needs a minimum basic understanding of drive file and directory organization and the best way to learn this is to open the command prompt and start typing. The command prompt is what old-timers refer to as the DOS prompt however the correct terminology in today's world is the CLI or Command Line Interface. Open a command prompt on your computer now by clicking on Windows' start button, type CMD in the search field and then press ENTER. You should see a window similar to Figure 1 below.

CLI
Figure 1 - The Command Line Interface

Note: There are a few different ways to open the command prompt: The method described above; pressing the Windows Key and R at the same time then typing in CMD and presssing ENTER or by clicking Start → Programs → Accessories → Command Prompt. (One of the biggest problems with Windows is that there are too many ways to do the same thing.)

- The Tree -

The method in which files and directories are set up on a drive is known as a hierarchical tree structure. The beginning of any drive is known as the root directory for that drive (in Windows directories are known as folders). In the case of Figure 2 below C:\ signifies the root of the C: drive because of the single backslash ( \ ) following it. A path points the way to any file or directory located on the drive. Figure 2 below shows what the path to File7 would look like (C:\Directory1\SubDirectory1\File7).

Hierarchical
Figure 2 - The hierarchical structure of drives

You may not have realized it until now but Windows has been displaying this same structure to users for years. Figure 3 below may be a familiar sight in the left pane of Explorer when navigating a drive.

Windows
Figure 3 - Windows representation of a drive's hierarchical structure

What you need to keep in mind is that command line interfaces have been around much, much longer than graphical user interfaces (GUIs aka Windows). So it was the Windowing operating systems, such as Windows and Linux, that needed to find a way to represent the underlying hierarchical drive structure to users. Furthermore, the interactions with folders and icons in a GUI can be equated back to the commands one would have typed into a CLI when GUIs were not present. These relationships will be highlighted when appropriate as new commands are introduced.

- HELP -

The first CLI command you should learn is HELP (by the way, commands in the Windows command prompt can by typed in upper or lower case or even a combination of both). Typing HELP at the command prompt will yield a screen of commands and a basic one-line explanation of each command. To get detailed help on a command you can type HELP, followed by a space, and then the name of the command. For example, at the command prompt type in HELP CD.

Help CD
Figure 4 - Getting help with the CD command

As you can see in Figure 4 above a detailed help screen for the CD command has been displayed. If the help is too much for one screen of information, the message "Press any key to continue . . ." will be displayed at the bottom of the page to signify that there is more help to follow by pressing a key. An alternative way to ask for help from any command is by adding the /? command switch to the end of the command. For example, typing in CD /? will yield the same series of help screens that HELP CD displayed.

- Navigating the Tree -

Before this lesson continues on you need to understand that working in the CLI can be very dangerous if you do not know what you are doing. You must follow the examples from here on out correctly and that means you need to read the instructions carefully. The CLI in the hands of a seasoned computer professional is one of the most powerful tools that professional has at their disposal. However, not taking care to learn the proper syntax of CLI commands can have disasterous results for the new-comer. Read this paragraph again before continuing on.

- Creating a Sandbox -

Before we can start examining commands we need to build a safe place to do this in called a sandbox. Type in each command below followed by the ENTER key at the command prompt.

c:
cd\
md Sandbox
cd\sandbox
md This
cd this
md is
cd is
md a
md another
cd a
md path
cd..\another

md path
cd\sandbox

Ok, at this point your command prompt should look like this:

C:\Sandbox>_

Don't worry, by the end of this mini-lesson you'll understand everything that happened by typing those commands in.


- TREE -

The TREE command shows the directory structure from the current directory location in a tree format. Type the command TREE in now at the command prompt.

Tree
Figure 5 - The TREE structure of the Sandox directory

As you can see in Figure 5 above the TREE command displays the structure of the Sandbox directory we created.

- Changing Drives -

Most computers have a minimum of two drives available, the hard drive usually referred to as the C: drive and a CD-ROM drive usually referred to as the D: or E: drive depending on your computer's setup. To switch between drives at the command prompt you simply type the drive letter followed by a colon ( : ). The first two characters of the command prompt itself will always indicate which drive you are currently logged onto:

C:\WINDOWS\SYSTEM32>_

The example command prompt above indicates to the user, with the first two characters, that it is currently logged onto the C: drive. If your computer has a D: drive you would simply type in D: at the command prompt and the prompt itself would change to reflect the new drive:

D:\>_

If you attempt to change to a drive letter that does not exist you'll get an error from the command prompt, "The system cannot find the drive specified." Also, the command prompt remembers the last location in the each drive's path. So if you were to type C: while logged onto the D: drive you would once again see the command prompt change to:

C:\WINDOWS\SYSTEM32>_

and not:

C:\>_

Because the former was the last location in the path previously given for that drive. The command prompt will always show the user where in the tree structure it is located for the drive it is currently logged onto.


- CD or CHDIR : Changing Directories -

The CD or CHDIR command is one of the most used commands at the command prompt. CD changes directories meaning that it allows you to move anywhere within the drive's tree structure by supplying a path to the command. Type the following command in at the command prompt:

cd\sandbox\this

Your command prompt should now look like this:

C:\Sandbox\This>_

When a backslach character ( \ ) immediatly follows the CD command you are telling the CD command to start at the root of the drive. The CD command is then told to open the sandbox directory followed by another backslash character. This second backslash character typically means that you want to open yet another directory, known as a subdirectory, located in the sandbox directory. The subdirectory we are instructing the CD command to open next is the this subdirectory. The command above can be read as:

"From the root of the current drive open the 'sandbox' directory and then the 'this' subdirectory."

Upon completing the command successfuly the command prompt will reflect the current location, or path, you specified. Type in the following command:

cd\sandbox\this\is\a\path

Bam, you just moved from one place on the drive to another in an instant with one command and your command prompt reflects this move:

C:\Sandbox\This\is\a\path>_

Let's go back to the sandbox directory from here. Can you figure out what the command will look like? Type the following command in:

cd\sandbox

You can use the CD command to move to a subdirectory, or a series of subdirectories, from the current directory instead of starting at the root each time. Type the following command in:

cd this

and your command prompt now shows your new path:

C:\Sandbox\This>_

By following the CD command with a space you are telling CD to move to a subdirectory from the current directory. You can even issue multiple subdirectories to move through. Type the following command in:

cd is\a\path

And once again, your command prompt displays the new path:

C:\Sandbox\This\is\a\path>_

You can also instruct the CD command to move backwards through a path. Type the following command in:

cd..

By following the CD command with two periods ( .. ) you are instructing the CD command to move back one directory level, otherwise known as the parent directory. Your command prompt once again shows the new path:

C:\Sandbox\This\is\a>_

You can even move backwards multiple levels. Type the following command in:

cd..\..

Here, you have told the CD command to move back one parent directory, and then from there move to the next parent directory. And once again your command prompt displays the new path:

C:\Sandbox\This>_

The CD command is a very powerful tool as it allows you to move anywhere within a drive's structure with just one command. In Windows, you need to open each folder one at a time by double-clicking on them, a slow tedious process for the computer professional that is meant for the every day user. In other words, GUIs slow us down because they are inefficient. Type the following series of commands in to prove this point:

cd\windows\system32
cd\sandbox\this\is\a\path

The first command moved you directly into the \WINDOWS\SYSTEM32 directory. The second command moved you directly to the \Sandbox\This\is\a\path directory. Now, let's examine what this process would have looked like using the GUI, or Windows:

Click on start
Click on Computer
Double-click the C drive
Double-click the WINDOWS folder
Double-click the SYSTEM32 folder

- ok, first command complete -

Click the back arrow button
Click the back arrow button
Double-click the Sandbox folder
Double-click the This folder
Double-click the is folder
Double-click the a folder
Double-click the path folder

- phew, second command now complete -

A complete waste of time! A computer professional is MUCH MORE efficient at the command line interface, period. What I find funny is when people watch a movie where a "hacker" or computer professional starts typing like crazy on a computer into a terminal, or CLI, window, people start snickering thinking, "yeah right, typing into a computer can do that". Yes, actually, it can!

And finally, if you want to go directly to the root of the drive you are currently logged onto issue the command:

cd\

Which can be read as , "change the directory to the root", because a single backslash character denotes root, and your command prompt now relects the change:

C:\>_

Let's get back into our sandbox before moving onto the next command. Issue the following command to do this:

cd\sandbox

- DIR : Viewing the Directory Contents -

Once you have reached your destination you'll more than likely want to know what is contained there. The DIR command is probably the second most used CLI command because it allows the user to view the contents of a directory, providing a directory listing of files and subdirectories. At the command prompt type in the following command:

dir


Contents of Sandbox
Figure 6 - Move along ... nothing to see here

The Sandbox directory is currently empty except for a three entries marked <DIR>. The first <DIR> entry has what appears to be a period ( . ) as a name. This entry means "the current directory". The next <DIR> entry has what appears to be two periods ( .. ) as a name. This entry means "the parent directory", one in which you are already familiar with because of using it with the CD command. The third <DIR> entry however is an actual subdirectory called "This". Any entry marked with <DIR> is a subdirectory branching from the current directory.

Type the following command in at the command prompt:

dir\windows


Contents of Windows
Figure 7 - What a mess

Ok, what just happened there? Well, once again let's break down the command in English terms to get a better understanding.

"Give me a directory listing of the WINDOWS directory by starting from the root of the drive then opening the WINDOWS directory"

Are you starting to see the power you have at the CLI? You can view a drive's contents, no matter where they are located, from anywhere you currently happened to be logged on the drive itself! Figure 7 above is showing us a listing of all the files and subdirectories located in the C:\WINDOWS directory. However, there are too many files and subdirectories to show in one screen and there is no real order to the chaos in which they are being displayed. The DIR command must be modified by use of command switches available to use with it. Type in the following command to obtain help on the DIR command:

dir /?

DIR Help
Figure 8 - The DIR help screens combined into one screen

As Figure 8 above highlights there are many options, or command switches, available to use with the DIR command to modify its default behavior. Let's view the \WINDOWS directory listing again, but this time use command switches to clean the output up a bit. Type fhe following command in:

dir\windows /ogn /p

Much better, right? The subdirectories were listed first, in alphabetical order, followed by the files, again in alphabetical order. Furthermore, the DIR command paused after each screen of information waiting for the user to press a key to continue to the next page. Let's break each switch down while referring back to Figure 8.

/ogn - [ o ] list files in sorted order [ g ] group directories first [ n ] by name (alphabetically)

/p   - Pauses after each screenful of information


- Redirection -

When you type a command into the CLI and press ENTER you expect the command's output to be displayed on the screen. However, you can redirect the output to a file for later viewing if you wish. Type the following commands in to see this in action:

cd\sandbox
dir\windows /ogn > dirlist.txt
dir


Redirection
Figure 9 - The directory listing of c:\windows redirected to a file

By using the redirection symbol ( > ) you can send a command's output to a file. Since the command prompt is currently residing at c:\sandox the file dirlist.txt was created there. Go ahead and use Notepad to open it by issuing the command:

notepad dirlist.txt

There are two redirection symbols available, ( > ) the greater than sign which creates a new file and ( >> ) two greater than signs together which means to append (add to) an existing file. If you use the single greater than sign to redirect to a file that already exists the file will be wiped first, so keep this in mind. If you wish to keep the contents of the file you must append to it instead.


- MD or MKDIR and RD or RMDIR : Pruning the Tree -

The MD or MKDIR command is used to create a new directory or subdirectory. Type the following commands in to see how this works:

cd\sandbox
md newdir
tree
dir


New directory
Figure 10 - A new branch was added to the tree

The RD or RMDIR command is used to delete a directory or subdirectory. Type in the following commands to see what happens:

cd\sandbox
copy dirlist.txt .\newdir
rd newdir
tree


Not as planned
Figure 11 - Well that didn't go as planned?

Before we attempted to remove the newdir subdirectory a file was copied into it (we'll discuss copying files in a bit). However, when attempting to delete the newdir subdirectory we were met with the message, "The directory is not empty." The RD or RMDIR command will not remove a directory or subdirectory unless it is empty. This is a safety feature to make sure you have not forgotten about the contents of the directory. However, this behavior can be overridden by using the /s command switch. Type the following commands in and answer Y to the question that appears:

rd newdir /s

tree

That's better
Figure 12 - newdir removed even with a file present

As an added safety feature you noticed that the RD command asked if you were sure you wanted to remove the newdir directory. This is because once a directory or subdirectory has been deleted it is lost forever as Windows will not move it to the recyle bin. This includes files and subdirectories contained inside the directory you are deleting!

Warning!: RD or RMDIR used with the /s switch can be very, very dangerous! Imagine (DON'T TYPE THIS IN) typing in rd c:\windows !!! In one swift move you have just deleted Windows from the hard drive and it is lost forever!