Absolute computer fundamentals: the command line!

So I’ve spoken to more than one DBA who is not familiar with using the command prompt (aka command line) – or as we old-schoolers still accidentally call it, “DOS”. It’s not really DOS any  more, of course, but it still uses the old commands and has the same basic look:

An empty command prompt window

From here, you can move around, change things, and explore. The command prompt is context dependent, meaning – just like in a file explorer window – you can see and affect things (mostly) in the folder (aka context) that you’re IN. In the image above, we’re on the drive root: C:\.

Why should it matter to use the command prompt? Well for one thing, certain things are just easier and/or faster via the command prompt, than it is when you use the Windows GUI.

For another thing, PowerShell accepts all the basic command prompt commands. I definitely think you need to learn PowerShell, so you may as well double up and learn your basic command prompt commands!

Launch your command prompt window: Of course you can find “command prompt” in the Windows menu. But you can also use Windows-R to get the Run dialogue, and type cmd. That’s how Jen does it, kids!

Basic Commands

You’re in the command prompt. Just like being in a File Explorer window, the command prompt has a context. You’ll likely start out in your own user folder. For example, C:\Users\jmccown. Let’s move around a little, shall we?

  • cd .. – Go up one directory level. (“..” always means “up one directory level. So if we wanted we could type ..\.. to go up TWO directory levels, and so on.)
  • cd \ – Go to the root directory. In this case, we’re in C:\, so wherever we are in the folder structure, “cd \” will take us back to the root of C:\.
  • cd <folder name> – Change directory to <folder name>. This is like double clicking on a visible folder in a file explorer window.
  • dir – Get a listing of what’s in this directory.
  • dir p* – Get a listing of everything in the current context (the folder we’re in) that starts with the letter P.
  • dir P* /A:D* – Get a listing of just the directories in the current context that starts with the letter P.  How did I know about that /A flag? See “Help”, below.
  • cd “c:\Program Files\Microsoft Office” – Move to the Microsoft Office folder, no matter where I’m starting from. See, I’ve given the command prompt a full path, so it’s not context dependent! Also note that I enclose the path in quotes, because the command prompt depends on spaces to tell one element from another. If you tried this without quotes, you’d get an error.
  • L: – change to the L drive. Note we do not cd (change directory), because L isn’t a directory!
  • Help <command> – Get help on the <command> named. This can be very, well, helpful!

Getting Help

Remember that /A attribute from above? We can find out what options we have, and what other flags are available for each command. Just use Help dir, or whatever command you’re interested in. For example:

command prompt with "help dir" results

You can also look up or study command prompt commands on TechNet and elsewhere.

More commands, and batch files

Well, we can rename (or ren) files and folders, move, copy, clear the screen (cls), direct output to a file (>, which creates or overwrites a file, and >>, which creates or appends to a file), and exit the command prompt.

You can also run batch files, which are just saved lists of commands. If I save the following list of commands as file.bat

cd \
cd temp
dir *.txt >> output.txt

…then I can run it from the command prompt, using file.bat, to get list of TXT files in c:\temp written to a new file called output.txt (or appended, if the file already exists).

This really is a bare bones introduction to the command prompt. But I really think you should familiarize yourself with these basics – play around with them. Just be careful not to rename, delete, or overwrite anything important!