How to Command Line When You Know Nothing
For those of us who can’t age ourselves by talking about the halcyon days of C64 and spelunking in the Great Underground Empire, and remembering the only thing useful about a GUI was to play Minesweeper, the command line may be an unfamiliar experience. What follows will hopefully help you visualize things as you start to muck about with the command line.
For this, I’m going to assume you are working with Windows.
OPENING UP CMD
To get to a command prompt, right click on the Start button and click Run, and type cmd
to open a command prompt.
This opens the command line interface (CLI) and should look similar to the picture below.
This also places the CLI’s C:\_
icon on the task bar. You can right-click this icon to pin it to the task bar if you want easy, one-click access in the future. When you right-click the icon, if you right-click Command Line again, you will get options to Run as administrator, should the need ever arise, or properties so you can customize your prompt to make it easier on the eyes (or even spoof a C64 if you so desire…).
NAVIGATING FOLDERS/DIRECTORIES
Starting out, we will assume a basic familiarity with Windows File Explorer, insomuch that you already know that each disk drive in the computer is assigned a different letter (e.g., C: is the drive the computer starts on when booting) and that everything is assembled into a hierarchy of folders. Folders and directories are the same thing in Windows; you can use the names interchangeably.
RUNNING COMMANDS/PARAMETERS
To run programs, we can type the name of the program followed by any parameters. Generally speaking, you must be in the same directory as the program to run it. If you’re not in the same directory, you need to either change directories, or specify the path. As an example, you can see what happens below if you try to run the combinator.exe from the C: drive while the combinator program itself resides on the H: drive. To switch drives, we type C:
to change to the C drive, D:
to change to the D drive, E:
to change to the E drive, etc. Note that any files or directories that include spaces in the name will need to be enclosed in quotes, otherwise the operating system thinks a space is a separator between multiple file names or parameters.
BASIC COMMANDS & NAVIGATION
There are some commands that are built into the Windows operating system pathing and so can be used anywhere. Some of the most fundamental commands are:
cd | change directory |
cls | clear screen |
copy | copy file |
del | delete file |
dir | directory listing |
echo | repeats whatever is typed |
exit | leaves the CLI |
help | lists a number of basic CLI commands |
md | makes a directory |
mkdir | makes a directory |
move | moves a file |
path | shows the current path |
rm | remove file |
rmdir | remove directory |
tree | shows a tree-like view of the current directory and all sub-directories |
type | types the contents of a text file to screen ( cat command in Linux) |
You can find full documentation of the built-in commands at https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands.
Directories are the exact same thing as folders in your normal Windows File Explorer. Directories are what originally existed when there was only DOS. They were renamed into Folders with Windows to create a graphical representation of directories.
Some useful default directories to know are .
and ..
A single period refers to your current directory. As far as I can tell, a single period isn’t all that useful in Windows anymore. Where it might be most useful is if you’re trying to run a command in the current directory but the computer thinks you’re trying to run a command by the same name in the system path. For example, if for some reason you’ve created a program and named it “cd,” to use your program “cd” rather than the change directory cd
, you may need to preface the command with .\
A double period refers to a parent directory, going up a folder. You can do cd ..
to go up a level
or use it in relative pathing like so:
That command says to go up one level from Test Folder (the current location), into Test Folder’s parent folder, CLI, and then run example2.py, which resides in the CLI parent folder. We never actually left Test Folder; we just told Windows where to find example2.py relative to our current location.
If relative directories were not a thing, you would have to type the full path.
One thing to keep in mind as you’re starting to type folder names and file names, once you’ve started typing, you can hit tab and it will try to autofill the rest of the text for you. If there are multiple options, you can continue typing tab until it selects the one you want. For example, if you start typing cd H:\te
and then hit tab, it will go in alphabetical order looking for any folder that starts with H:\te
.
GETTING HELP
Simply typing help
will generate a list of basic CLI commands.
Generally speaking in Windows, adding /?
at the end of a command will bring up a help file for that command (like how --help
works in Linux) and list what parameters can be used with it.
In Windows, each parameter will start with a /
. As an example here, if we look at dir /?
, we can see the different options available for listing a directory.
If we are interested in listing the directory contents in alphabetical order, we would use dir /ON
, but for reverse (descending) alphabetical order, we would use dir /O-N
, and then we could display a bare listing with dir /O-N /B
.
PIPING
Another very useful tool is piping (>
). This is used to take the output of one program and send it somewhere, either to use as the input for another command or to become the contents of a text file. If we are saving the output to a file, if we use a single >
, that will replace the contents of that file with the new input. If we use >>
, that will append the data to the end of that file (while still keeping all the original file contents). Here we will demonstrate using dir /O-A /B
and, instead of printing it to the screen, let’s save it to a file.
PATH ENVIRONMENT
From a Windows perspective, you don’t really have to worry about pathing too much anymore when you’re using the Windows GUI. If you spend much time in the CLI or do a lot of programming, you’re going to want to learn path environments. This is what enables you to use the program from anywhere without having to type the path. You can type path
to see what is included in your system’s master path.
You can see your Windows master path environment by looking at System Properties/Advanced/Environment Variables. However, this is not the same as your CLI path. Any changes you make to the CLI path by using the path
command will last only until you close the CLI, at which point they will revert back. If you want to actually edit what is in your CLI path, this is found in the registry at Computer\HKEY_CURRENT_USER\Environment\Path, or by using the setx
command. If all this is new and foreign, ignore this section—messing around in the registry or with the setx
command without understanding what you’re doing puts your Windows installation at risk.
This all really is only the very basics of getting started with Windows CLI. (Well, I guess messing with the registry crosses the line from the very basics to a bit more serious, but, as you learn CLI, it won’t take too long before you’re wanting to learn pathing to simplify your life).
ADDITIONAL TOOLS
The Sysinternals Suite is a series of commands that, once upon a time, were included in Windows, but have been split into a separate download. You can explore the individual tools (which include things like hex2dec
or strings
), or download the entire suite in one go, at https://docs.microsoft.com/en-us/sysinternals/downloads/sysinternals-suite.
It is perhaps easier to learn CLI on a system you are familiar with (in this case Windows), but once you are comfortable with the basics of navigation and running commands with parameters, you will find there are a lot of similarities between Windows CLI and Linux CLI, and it becomes far easier to feel comfortable in any CLI. Many of the Linux GNU tools you will hear about (like cat
, sed
, awk
, grep
, etc.) have been ported to Windows, and you can search for a flavor that appeals or shift into the Windows Subsystem for Linux.