Extras
Basic Command Line Instructions on Windows
When we turn on a Windows PC, we usually tell it what to do using the Windows graphical user interface. But, you can also give a computer instructions by typing text into a command line interface.
- Graphical user interfaces (GUIs) let users tell a computer what to do using visual representations and symbols. For example, the user can point a mouse at an icon and click on it.
- Command line interfaces (CLIs) let users tell a computer what to do by typing instructions (or commands).
Developers often need to run tools that do not have GUIs; therefore, they need to know how to use the command line.
Windows has a program called the Command Prompt which provides a command line interface for Windows.
You can type a set of Windows commands into it, and it will perform the tasks that the commands instructed it to. When you run some commands, a response may be shown (if so, this will be in text).
To run the Windows commands, Command Prompt implements something called the Command shell, which is why you might hear the Command Prompt called a shell (it is also similar to shells on the Unix operating system). Other names for the Command Prompt include the command line or cmd.exe.
Opening Command Prompt
To open the Command Prompt:
- Type
cmd
into the start menu then press enter or return. - Or press Win + R to open the Run window, type
cmd
into the text input, then press the OK button.
When Command Prompt is opened, it will show information about the version of Windows you are running.
Below this you will see the prompt. It is made up of:
- A path to a folder on your computer
- Followed by the greater-than (
>
) symbol
When you start Command Prompt, the prompt will show the path to your Users
folder.
When you work with files on Windows, Windows Explorer provides a graphical representation of the files and folders on your computer.
When you work with Command Prompt, the prompt tells you which folder you are working in. This is known as the current working directory.
Knowing the current working directory is important because many of the commands you use in Command Prompt either affect the working directory or the files it contains. (Later on this page, you will learn how to navigate between folders on your computer using Windows commands.)
Using Basic Commands
At the prompt enter the command: dir
Then press the enter or return key.
dir
is short for directory.
It will show the files and folders in the current working directory.
Next, enter the command: cls
Then press the enter or return key.
cls
stands for clear screen.
It removes all the information in the window.
Then it just displays the prompt.
This is very helpful because the window can easily get cluttered.
Learning More About a Command
Enter the command dir
followed by /?
Then press the enter or return key.
Adding /?
after the name of a command will show you information about what the command does and how to use it.
As this screen shot shows, commands can have additional options to control how they work. Some of the options have attributes (which are like settings for that option).
Once you have found the information you want on this page, press any key to go back to the prompt.
Using Commands with Options
Enter the dir
command.
Add the /B
option after it.
dir /B
Then press the enter or return key.
The /B
option tells Command Prompt to just show the bare file or folder name.
Using Commands with Multiple Options
Enter the dir
command.
Add the /B
option.
Add the /L
option.
dir /B /L
Then press the enter or return key.
The /L
option tells Command Prompt to show names in lowercase.
Using a Command with an Option that has an Argument
Enter the dir
command again.
Followed by the /O
option.
Then add the :D
argument after the option.
dir /O:D
Then press the enter or return key.
The /O
option orders the results.
The :D
argument says they should be ordered by date.
TRY: Change the argument to /O:-D
.
The minus symbol before the D
reverses the order.
Enter the dir
command again.
Add the /B
option.
Then the /O
option.
Add the argument :-N
after the /O
option.
dir /B /O:-N
Then press the enter or return key.
The /B
option shows the bare file or folder name.
The /O
option orders the results.
The :-N
argument orders them by name in reverse.
Moving Around the File System
When using a GUI like Windows Explorer, you can click on folders to navigate around the directories and files on your computer.
When using Command Prompt, you move to a different folder using the cd
command, followed by the path of the folder you want to move to.
The path shown in the prompt is the current working directory.
You often need to navigate to the correct directory before running a command to ensure that the command is run on the files or folders that are in the current working directory.
Changing Directories
Enter the cd
command, followed by the path to a folder you want Command Prompt to move to. This will make the folder specified in the path the working directory. For example:
cd C:\Windows
Then press the enter or return key.
This makes the C:\Windows
folder the working directory.
NOTE: The path in the prompt has been updated so that it shows the current working directory.
To specify a folder or filename that contains a space, place double quote marks around the name or file path. E.g. C:\"Program Files"
Here are more examples of using the cd
command. The paths:
- Can be absolute or relative paths
- Are not case sensitive
- Can be created by dragging a folder from Windows Explorer onto the Command Prompt window.
- Can use an autocomplete feature; enter 3-4 letters of a folder name, then press tab and it will autocomplete the rest of the file or folder name
Argument | Description |
---|---|
cd\ |
Top-most directory of current drive |
cd C:\ |
C drive's root folder |
cd D: |
Change to D drive |
cd .. |
Go up one directory |
cd FolderName |
Relative path to child folder FolderName |
cd C:\Users |
Absolute path to C:\Users |
cd C:\"Program Files" |
Put names with spaces in double quotes |
Create, Copy, Move and Delete Files
When using a GUI like Windows Explorer, you can use keyboard shortcuts such as ctrl + c to copy, ctrl + v to paste, and you can drag files between folders to move them.
To create, copy, paste, or move files and folders using Command Prompt, you need to use the following commands. Each one uses path(s) to the file or folder as an argument.
To see the following examples working:
- In Windows, go to your user home directory
- In Command Prompt, go to your user home directory in the
C:\Users
folder
Command | Description |
---|---|
mkdir |
Create directory |
echo |
Create text file |
copy |
Copy |
move |
Move |
del |
Delete file |
rmdir |
Remove directory |
tree /f |
Show tree view |
Creating a Directory
Make sure you are in your users folder.
Enter the mkdir
command.
Add the argument test
mkdir test
Then press the enter or return key. If you look in Windows Explorer when this command is run, you will see the folder called test
appear.
TRY: Use the same command to create a folder called test2
.
(This will be needed in a later example.)
Creating a Text File
Enter the echo
command.
Then add some text.
Then add a greater-than symbol >
(called the redirection operator).
Then the path test/test.txt
echo This is a test file > test/text.txt
Then press the enter or return key.
Look in Windows Explorer when this command is run and you will see a text file called test.txt
is created in the test
folder, it will contain the text This is a test file
.
The redirection operator >
is used to send the specified text to the named text file.
Copying a File
Enter the copy
command.
Then the path to the file to copy.
Then the path to where the file should be copied.
copy test/text.txt test2/text.txt
Then press the enter or return key.
If you created the test2
folder earlier, this should copy test/text.txt
to test2/text.txt
.
If you did not, you will see a message saying The system cannot find the path specified
.
Deleting a File
Enter the del
command.
Followed by the path to the file to remove:
del test2/text.txt
Then press the enter or return key.
This will delete the test2/text.txt
file.
NOTE: This command can delete ANY file you specify.
It will not ask you to confirm before deleting it.
Moving a File
Enter the move
command.
Then the path to the file to move.
Then the path to where it should be moved.
mv test/text.txt test2/text.txt
Then press the enter or return key.
This will move the file test/text.txt
to test2/text.txt
.
Command Prompt will tell you what has been moved.
Deleting a Folder
Enter the rmdir
command (remove directory).
Then the path to the folder to delete.
rmdir test2
Then press the enter or return key.
If the folder is empty, this will delete the test2
folder.
If the folder contains any items, you should add the /S
option.
This deletes the directory and any files or subdirectories.
You will be shown a prompt to confirm that you want to do this.
Finding a File or Folder
Enter the dir
command.
Then add the name of the file or folder you are looking for, e.g., text.txt
.
Follow this with /s
to indicate you want to look in subfolders, too.
dir text.txt /s
This will show files and folders in the current working directory (the path shown in the prompt) and any child folders (because the /s
flag was added) that use the specified search term.
If you are not sure of the file or folder name, you can use an asterisk *
symbol as a wildcard, to indicate one or more missing characters.
Term | Description |
---|---|
text* /s | Find files or folders starting with text (e.g.: text.png , textures.jpg or textiles.txt ) |
*ing.txt /s | Find text files ending with ing.txt (e.g.: ending.txt , coding.txt or encoding.txt ) |
*.jpg /s | Find files with the .jpg file extension |
\*ext.txt /s | Find file ending ext.txt anywhere on the current drive |