Technical Literacy Command Line Windows
Command Prompt
Access the Mac command line by starting Command Prompt, which you can find in the start menu. Sometimes you'll need to run the command prompt as an administrator - such as when installing programs or packages. To do this, right-click on the Command Prompt icon in your start menu and select Run as Administrator.
Prompt
When you first open the command prompt, you might see something like:
Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Users\alice>_
This is called the prompt. You'll see a blinking vertical line, called the cursor. This shows where the characters you type will appear, and also let you know the prompt is ready to receive input (if a program is running, you won't see it).
The prompt also gives you some useful information. Here, we can see that we're in the c:\Users\Alice
directory, which is our home directory.
Have a look at your command prompt now and see if you can extract these details from your own prompt.
At the prompt, you type (or paste in) a command which runs when you press ENTER.
Running programs, specifying options
Normally at the terminal you run programs or scripts which do something for you. For example, to count the number of words in a file. Because programs often need some kind of input (such as a name of a file or directory to process) we have to provide parameters (or arguments or options).
It's typical that this essential parameter - the thing you want the program to work with - is typed after the name of the program. So to use the program type
to list the contents 'essay.txt', we could try something like the following:
C:\Users\alice> type essay.txt
My essay
Rather than telling a program what data to process, we sometimes want to tell it how to process data, or how to do what it is supposed to do. These are usually specified as parameters (or arguments or options), which are passed to the program.
You can usually find out what options are available using the program's inbuilt help /?
:
tasklist /?
The documentation will describe the program's parameters and give some examples of usage.
Paths
In Windows, the root (or utmost top-level) of your file system is a drive. In most cases, this will be the C
drive, represented as c:\
. Usually all your system files and data are on your C drive.
If you plug in a USB drive you'll will have additional drives which you can move to.
Normally, you'll be working on files within your user directory, which will be something like c:\Users\alice
Each file and directory has a unique path. The path is made up of the location within the file system tree as well as the name of the file or directory. Eg, my music directory might have the name music
, and be located at c:\Users\alice
, therefore have the absolute path of c:\Users\alice\music
Tips
- When you are working with directories in a graphical user interface, they will likely be referred to as a folder. Same thing, different name and representation.
- If you have a file or directory selected in Explorer, right-click on it and select Properties to see its path.
- Drag and drop a file or folder from Explorer into the command prompt to insert its full path.
- As you change directory, type the first letter of a path and press the TAB button on your keyboard. This allows you to 'tab complete' the path - a way of avoiding typing and also to probe out a path name
Relative paths
Any path that doesn't start with a forward slash is a relative path, meaning that its actual location depends on where you are when you refer to it. Relative paths are essential because otherwise we'd always have to type absolute (or full paths. Note that %HOMEPATH%
points to your home directory's absolute path.
For example, in my home directory, let's say I have documents, music, and photos subdirectories. If am at my home directory, I can type cd music
to change into the music subdirectory (so I will be at %HOMEPATH%\music
). When I use the relative path 'music' as part of cd music
, the system needs to resolve it to an absolute path. In this case, because there is a subdirectory called 'music' in our current directory, it knows what we mean by that relative path.
If the system can't resolve a relative path, you'll get an error message along the lines of 'The system cannot find the path specified.'.
You can traverse as many levels down the file system tree as you like: cd %HOMEPATH%\music\r\radiohead\amnesiac
You'll also want to go 'up' the file system tree, to shift your location to the parent directory of the directory you are in. You can use the special reference ..
for this. ..
always means 'up one level from the current path reference'.
For example, if we're deep down in our music:
C:\Users\alice\music\r\radiohead\amnesiac> cd ..
C:\Users\alice\music\r\radiohead> _
You can use several of these references to jump multiple levels up:
C:\Users\alice\music\r\radiohead\amnesiac> cd ..\..\..\
C:\Users\alice\music\> _
Orientation
At the command line, your activity and the commands you run are in relation to the "current working directory". That is it say, you are always "standing" somewhere in your file system.
The Windows command prompt always tells you where you are in the prompt itself:
C:\Users\alice>_
Here, we can see we are "in" the C:\Users\alice
directory.
Listing Files
dir
displays the files and directories. If you don't give dir
a path, it will display the contents of the current directory.
dir
C:Users
directorydir c:\Users
Working with directories
In Windows, the backslash \
is used to separate paths. This is different from the Mac/Unix world which uses a slash /
.
Changing directory
The cd
(change directory) command is used to, well, change directory. This allows you to change where you are 'standing' at the command line.
cd %HOMEPATH%
cd Documents
cd c:\Users\Alice\music
Making
mkdir
will make a directory
mkdir test
mkdir c:\Users\alice\documents\test
Deleting directories
rmdir /s
(remove directory) will delete a directory.
rmdir /s test
rmdir /s c:\Users\alice\documents\test
Note: Be very sure of the path you give to rmdir
before you press ENTER
Working with files
Copying
Use copy
(copy) to copy. You have to provide a source and destination path.
copy test.txt copy.txt
copy test.txt c:\Users\alice\documents\copy.txt
You can copy entire directories (and their contents) with the xcopy
command.
xcopy music music2
Moving and rename
Use move
move/rename files and directories. You have to provide a source and destination path.
move test.txt copy.txt
move test.txt ..
You can also move directories (and their contents).
move music music2
move music old/
Deleting
Use del
(del) to delete files.
del test.txt
Elevated permissions
Some commands need a higher level of access to run. To do this, right-click on the Command Prompt icon in your start menu and select Run as Administrator.
You shouldn't need to use the administrator command prompt on a everyday basis. Remember that anything you run (or any mistakes you make) could be more drastic.
Tips
- Pressing the up and down arrow keys on your keyboard allow you to flip through commands you've previously typed.
- You can drag and drop a folder or file from Explorer to the Command Prompt to insert its path.
- You can stop a currently running command with CTRL+C (forcing it to exit).
cls
Clears the contents of the window- Holding SHIFT while right-clicking a folder in Explorer will give you the option to open a command prompt at that location
If you find yourself working in shell a lot, consider using PowerShell (built-in to Windows) or cmdr for a more powerful experience.