Getting Started with Windows Command Prompt
Developer or not, there’s no doubt you came across a Command Prompt — that dark window with cryptic text that creeps everyone. Today we’ll face those fears. Starting with simple steps to more advanced topics, this comprehensive guide to the world of CMD will take you from novice to ninja in no time.
Although this guide is primarily aimed at the Windows audience, I’ll also mention similarities to the Linux terminal so you can draw parallels between the two. I’m using Windows 10.
Other episodes in the series
What is Command Prompt?
Command Prompt, often referred to as Bash or Terminal is a command-line interface program used to execute commands on the operating system. Its purpose is to manage our computers from a single window using code.
Everything from navigating directories and managing files to booting, scheduling tasks, writing custom scripts, setting up networks, and installing apps is just a niche of what Command Prompt can do.
Let’s dive right in.
Navigation
To open a Command Prompt on your PC, look for search and type CMD.
Click on the first option and it should display the Command Prompt window located in your root directory.
For easier understanding, this is the same root directory in the Explorer view.
Basically, the Command Prompt is another way to going to navigate the computer, manage files and folders, and many other things using special commands that we’re going to learn today.
List directories
To start let’s list all the files and folders in the directory we’re currently in. The command that can achieve that is dir
for Windows or ls
for Linux. Since we’re using Windows we’ll use the former.
> dir
This lists all the files and folders in the root directory, just as we’ve seen in the Explorer view above.
Change Directory
To enter a specific directory we type cd
followed by the name of the directory.
> cd desktop
Notice here that I’m typing all lowercase (desktop), even though the folder name is in a capital case (Desktop). Unlike Unix terminals where we have to be specific about casing, on Windows CMD casing does not matter most of the time. So we could type
CD DESKTOP
and it would produce the same result.
As we entered a new directory we can notice that the path has changed from Users\Mirza
to Users\Mirza\Desktop
. This means that we moved up one directory.
We can once again use dir
command to list all directories here as well.
Auto-Complete
Within the Desktop directory, we have three more directories, cars, colors, and movies. We could enter any by typing cd <folder-name>
but that’s a bit slow, especially if we had folders with really long names.
What we can do is type cd
and then hit the tab
key. What this will do is toggle between directories within the current directory.
Desktop> cd + tab
Desktop> cd cars
Desktop> cd colors
Desktop> cd movies
Neat trick. It also works if we type a few letters of the directory we want to enter, e.g:
Desktop> cd c + tab
Desktop> cd cars
Desktop> cd colors
Now we only toggle between directories starting with the letter C.
Finally, we hit enter and we should enter the selected directory.
Back to the previous directory
We went into another directory. We could go deeper, but let’s learn how to go back. To go back to the previous directory we use cd ..
.
And here we’re in the Desktop directory again. Repeating the same command we should find ourselves back in the root directory.
The command cd..
also works on Windows, while cd ..
works both on Windows and Unix.
Change multiple directories
So far we’ve been navigating one directory at a time, forward and back. If we know the exact path to a specific directory we can use it to navigate multiple directories at once.
> cd Desktop/cars/golf
The same applies to navigating back. This time instead of writing folder names we’re going to use cd..
followed by /
. Each slash takes us back down one directory.
> cd../../..
Change to the drive root directory
I’ve already mentioned that the CMD first opens in the root directory of our current logged-in user (in my case C:\Users\Mirza), but we can go further back to the drive directory.
> cd /
Now we’re at the beginning of the C drive.
Change drives
If you noticed I’m actually in C drive on my machine. If you have multiple drives (C, D, E), you can switch between them. Let’s go to D drive.
> D:
As you can see now we’re at D drive. To go back to C drive we just input C:
> C:
Current Working Directory
Sometimes we may want to check in which directory we’re in or copy the path of the directory. For such cases, we make use of cd
command alone (pwd
on Unix) that does just that.
> cd
C:\Users\Mirza
This prints the path of the working directory, starting from the root.
Tree
We’ve already acknowledged that the folder Desktop has a lot of subfolders within. From cars to colors and movies.
C:\Users\Mirza\Desktop>dir
03/05/2023 11:15 AM <DIR> .
03/05/2023 11:15 AM <DIR> ..
03/05/2023 11:22 AM <DIR> cars
03/05/2023 11:20 AM <DIR> colors
03/05/2023 11:18 AM <DIR> movies
Using the tree
command we can list all the sub-directories as the tree structure.
C:\Users\Mirza\Desktop>tree
├───cars
│ ├───audi
│ │ ├───car parts
│ │ └───paint
│ ├───bmw
│ ├───citroen
│ ├───ferrari
│ ├───ford
│ ├───golf
│ ...
│ └───renault
├───colors
│ ├───black
│ ├───blue
│ ├───brown
│ ├───green
│ ├───orange
│ ...
│ └───yellow
└───movies
├───Avengers
├───Batman
...
└───Undisputed
We can narrow down the results by using the find flag /f
followed by the name of the folder (cars) we want to inspect.
Mirza\Desktop>tree /f cars
├───audi
│ ├───car parts
│ └───paint
├───bmw
├───citroen
├───ferrari
├───ford
├───golf
├───honda
├───hyundai
├───lamborghini
├───mazda
├───mercedes
├───mitsubishi
├───nissan
├───passat
└───renault
Create Read Update Delete
Now let’s learn how to create and manage files and folders.
Create Directory
To create a directory we use a mkdir
command followed by the name of the directory.
> mkdir games
A newly created directory should appear in the list.
C:\Users\Mirza\Desktop>dir
03/05/2023 05:31 PM <DIR> .
03/05/2023 05:31 PM <DIR> ..
03/05/2023 11:22 AM <DIR> cars
03/05/2023 11:20 AM <DIR> colors
03/05/2023 05:31 PM <DIR> games
03/05/2023 11:18 AM <DIR> movies
0 File(s) 0 bytes
6 Dir(s) 100,709,150,720 bytes free
On Windows, we can also use md
command to create directories. Let’s create a folder that has multiple words. To achieve this we need to wrap the directory name with quotes.
Desktop> mkdir "Programming Languages"
Create Multiple Directories
Using the same mkdir
command we’ve used above we can create multiple directives in line one after another.
Programming Languages>mkdir java javascript csharp golang vb php python
Programming Languages>dir
03/05/2023 05:44 PM <DIR> .
03/05/2023 05:44 PM <DIR> ..
03/05/2023 05:44 PM <DIR> csharp
03/05/2023 05:44 PM <DIR> golang
03/05/2023 05:44 PM <DIR> java
03/05/2023 05:44 PM <DIR> javascript
03/05/2023 05:44 PM <DIR> php
03/05/2023 05:44 PM <DIR> python
03/05/2023 05:44 PM <DIR> vb
Create a File
To create a file we make use of cd
command followed by the current directory .
greater than >
and the name of the file. The Unix variant is touch hello.txt
.
Programming Languages>cd. > hello.txt
Now we should see a hello.txt text file in the directory.
Programming Languages>dir
03/05/2023 05:59 PM <DIR> .
03/05/2023 05:59 PM <DIR> ..
03/05/2023 05:44 PM <DIR> csharp
03/05/2023 05:44 PM <DIR> golang
03/05/2023 05:59 PM 0 hello.txt
03/05/2023 05:44 PM <DIR> java
03/05/2023 05:44 PM <DIR> javascript
Another way to create a file is by using the echo
command. This method allows us to insert text into a newly created file.
The text in between echo
and >>
symbols will be inserted into the file (Pascal.txt) we’re creating.
> echo WriteLn("Hello from Pascal!") >> Pascal.txt
We can verify that we have a new file in our folder using the dir
command.
> dir
03/05/2023 05:44 PM <DIR> csharp
03/05/2023 05:44 PM <DIR> golang
03/05/2023 05:59 PM 0 hello.txt
03/05/2023 05:44 PM <DIR> java
03/05/2023 05:44 PM <DIR> javascript
03/05/2023 05:59 PM 32 Pascal.txt
03/05/2023 05:44 PM <DIR> php
03/05/2023 05:44 PM <DIR> python
03/05/2023 05:44 PM <DIR> vb
Preview Content of File
This one is as simple as it sounds. Use type
command
(cat
Unix variant) followed by the name of the file to preview.
> type Pascal.txt
WriteLn("Hello from Pascal!")
Content from the file gets printed in the terminal. It also works on multiple files.
> type Pascal.txt hello.txt
Pascal.txt
WriteLn("Hello from Pascal!")
hello.txt
Hello World!
Open Text files
We’ve already learned that we can preview text files using type
command, however, Windows does not allow editing files directly through the Command Prompt.
What we can do is open a file using notepad (or any other text editor)
From this point, the file is fully editable (unless it’s set to read-only in the properties).
If the file does not exist, notepad will prompt us to create a new file and then allow us to edit it.
If you’re using Visual Studio Code, you can open text files in it.
Just like before with start .
, we can use code .
to open an instance of Visual Studio Code in the current directory.
You can also find online versions of Nano and Vim that can run on Windows and use either to edit files directly through the Command Prompt.
Rename
To rename files and folders, we use the rename
command (or ren
shorthand), followed by the old name, then the new name.
Animals> dir
03/05/2023 08:04 PM <DIR> cat
03/05/2023 08:04 PM <DIR> dog
Let’s change the name of the cat folder.
Animals> rename cat kitty
Animals> dir
03/05/2023 08:04 PM <DIR> dog
03/05/2023 08:04 PM <DIR> kitty
The same applies to files.
> dir
03/05/2023 08:06 PM 0 file.txt
> rename file.txt document.txt
> dir
03/05/2023 08:08 PM 0 document.txt
With files, we can also change the extension.
> rename document.txt document.docx
> dir
03/05/2023 08:09 PM 0 document.docx
Delete File
To delete a file we use del
command followed by the name of the file.
> del Pascal.txt
We can verify that the file is gone.
> dir
03/05/2023 05:44 PM <DIR> csharp
03/05/2023 05:44 PM <DIR> golang
03/05/2023 05:59 PM 10 hello.txt
03/05/2023 05:44 PM <DIR> java
03/05/2023 05:44 PM <DIR> javascript
03/05/2023 05:44 PM <DIR> php
03/05/2023 05:44 PM <DIR> python
03/05/2023 05:44 PM <DIR> vb
We can also delete files within folders other folders.
For example, there is a color.txt file inside “bmw“ folder.
tree /f cars/bmw
DESKTOP\CARS\BMW\color.txt
Running del
command against this directory will delete all files within this directory.
Desktop>del cars\bmw
Desktop\cars\bmw\*, Are you sure (Y/N)? y
First, we’ll get a prompt that will ask if we want to do it or not. If we choose yes, all files within “bmw“ directory will be deleted.
C:\Users\Mirza\Desktop>tree /f cars/bmw
C:\USERS\MIRZA\DESKTOP\CARS\BMW
No subfolders exist
The file is gone.
Let’s use the same command against multiple files.
Desktop> tree /f cars/ford
C:\USERS\MIRZA\DESKTOP\CARS\FORD
performance.txt
speed.txt
This time we’re going to add two additional commands, /Q
(no question prompts) and /S
(look within subdirectories)
Desktop> del cars\ford /Q /S
Deleted file - C:\Users\Mirza\Desktop\cars\ford\performance.txt
Deleted file - C:\Users\Mirza\Desktop\cars\ford\speed.txt
Both files are deleted.
Delete Folder
To remove a directory we use rmdir
command (or rd
for short).
Desktop>rmdir games
Desktop>dir
03/05/2023 11:22 AM <DIR> cars
03/05/2023 11:20 AM <DIR> colors
03/05/2023 11:18 AM <DIR> movies
03/05/2023 09:01 PM <DIR> Programming Languages
03/05/2023 07:57 PM <DIR> snake
The directory “games” is deleted.
However, this command will not work on the “cars” directory as it is not empty. To remove it we’ll make use of the previous combination of /Q
and /S
to delete a directory as well as its subdirectories.
Desktop>rmdir cars /Q /S
Desktop>dir
03/05/2023 11:20 AM <DIR> colors
03/05/2023 11:18 AM <DIR> movies
03/05/2023 09:01 PM <DIR> Programming Languages
03/05/2023 07:57 PM <DIR> snake
Copy
The next thing to learn is how to copy files from one directory to another.
Desktop>dir
03/05/2023 07:49 PM 0 blue.txt
03/05/2023 09:34 PM <DIR> cars
03/05/2023 11:20 AM <DIR> colors
03/05/2023 11:18 AM <DIR> movies
Let’s copy our file blue.txt
into the colors directory. (copy
, file
, directory
)
>copy blue.txt colors
1 file(s) copied.
We can check that the blue.txt file is in Desktop, but it’s also in the colors directory.
Desktop>dir
03/06/2023 07:49 PM 0 blue.txt
03/05/2023 09:34 PM <DIR> cars
03/05/2023 08:53 PM <DIR> colors
Desktop>cd colors
colors>dir
03/05/2023 11:19 AM <DIR> black
03/05/2023 11:18 AM <DIR> blue
03/05/2023 07:49 PM 0 blue.txt
03/05/2023 11:20 AM <DIR> brown
03/05/2023 11:19 AM <DIR> gold
Move
I added a song to the Desktop and I want to move it to the music directory.
Desktop> dir
03/08/2023 09:34 PM <DIR> cars
03/05/2023 11:20 AM <DIR> colors
03/05/2023 11:18 AM <DIR> movies
03/05/2023 09:15 PM <DIR> music
03/06/2023 08:15 PM 0 song.mp3
To do that we use the move
command where we specify the file name, followed by the path location or the name of the directory which we want to move our file into.
Desktop>move song.mp3 C:\Users\Mirza\Desktop\music
1 file(s) moved.
Now let’s enter the music directory and verify that the file is there.
Desktop>cd music
music>dir
03/06/2023 08:25 PM 0 song.mp3
Start
The Start command is used to launch programs through Command Prompt.
If we open up a Command Prompt, type start
, and hit enter, it will launch another Command Prompt instance.
If we create a new directory (e.g. greetings) and then type start greetings, it will open a new explorer window in that directory.
If we want to open an explorer in the current directory we type start .
We can also open explorer in the previous directory using start ..
Launch programs
Inside the Command Prompt, we can type start chrome
and hit enter and it will launch a new Google Chrome window.
> start chrome // make sure you have Google Chrome installed
To open a particular website in Chrome you can type it in and hit enter.
> start chrome www.google.com
If Chrome is already open, it will display the content in a new tab of the existing window. We can also tell it to open in a new window:
> start chrome --new-window www.google.com
Or incognito Window
> start chrome --new-window --incognito www.google.com
If we just type in start www.google.com
it will open up the page in the default browser. In my case, that’s Google Chrome.
Run the Executables
To run an executable through Command Prompt, type the name of the file followed by the extension and hit enter.
If that does not work, prefix the command with start:
> start my-video.mp4 // opens a video in a default media player
> start Steam
> start ...
Commands for everyday use
Clear Command
By now our Command Prompt is full of the commands we entered. To clear the prompt we use the cls
command (clear
on Unix)
Before
After
Clear current input
Sometimes we type a command and then we change our mind right before we execute it. Use esc
key to remove the text in the prompt all at once.
Desktop>Some Command We Decided Not To Run
Esc
Desktop>
Otherwise use backspace to delete content by letter.
See the Previous Commands
We can use the arrow keys (Up and Down) to toggle previously entered commands:
- Arrow Up — previous command
- Arrow Down — recent command
To see a list of previously used commands hit F7.
Find String (findstr)
Imagine if we have lots of files or folders in a single folder and are looking for a particular file. Using findstr
we can search for it.
Downloads> dir | findstr download.html
07/10/2022 09:45 PM 350,831 download.html
We can also type just part of the name of the file and CMD will filter all matching files.
Downloads> dir | findstr downl
01/12/2023 07:10 PM 32,463 download (1).html
01/21/2023 11:01 PM 196,648 download (2).html
07/10/2022 09:45 PM 350,831 download.html
Let’s search for cars directories containing the letter m,
Desktop\cars>dir | findstr m
03/05/2023 11:20 AM <DIR> bmw
03/05/2023 11:22 AM <DIR> lamborghini
03/05/2023 11:21 AM <DIR> mazda
03/05/2023 11:22 AM <DIR> mercedes
03/05/2023 11:21 AM <DIR> mitsubishi
Copy output to clipboard
Rather than copying the output from the CMD manually, we can make use of the clip command that does copy for us.
Desktop\cars>dir | clip
Notice here that there isn’t any output, because it’s saved in the clipboard.
If we open up a text editor and hit paste, it will display the retrieved information.
This can be useful when we want to copy the whole output from commands like a list of directories, IP information, system settings information, etc.
Copy & Paste
If you’re a seasoned PC you’re probably used to copying and pasting using a keyboard, CTRL + C to copy and CTRL + V to paste.
You can highlight some text using the mouse or a combination of Arrow keys and Shift key, copy and then paste.
However, there is ambiguity with CTRL + C because it’s used for multiple things. Besides copying, the CTRL + C combination is also used to terminate the process (application) running through the CMD.
Here are safer ways to copy and paste:
- CTRL + INSERT (Copy)
- SHIFT + INSERT (Paste)
To print text into the Command Prompt we use the echo
command.
Pause
To pause the execution of the command prompt we use pause
command. Once executed, the prompt will show the message “Press any key to continue….” to the user and wait for the user’s input.
Run multiple commands in-line
Rather than executing command line by line, we run multiple at once. We just need to split each with an ampersand (&).
For example, we can create a directory and enter into it within the same command.
Desktop> mkdir music & cd music
Now we’re in the folder we’ve just created without needing to hit enter twice.
See the list of CMD commands
It’s impossible to know all possible commands and we do not need to, because CMD provides documentation for each command.
Type the command followed by /?
and it will print all options that can be used on that command.
>dir /?
Displays a list of files and subdirectories in a directory.
DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
[drive:][path][filename]
Specifies drive, directory, and/or files to list.
/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files I Not content indexed files
L Reparse Points O Offline files
- Prefix meaning not
/B Uses bare format (no heading information or summary).
/C Display the thousand separator in file sizes. This is the
default. Use /-C to disable display of separator.
/D Same as wide but files are list sorted by column.
/L Uses lowercase.
/N New long list format where filenames are on the far right.
/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/P Pauses after each screenful of information.
/Q Display the owner of the file.
/R Display alternate data streams of the file.
/S Displays files in specified directory and all subdirectories.
...
Run CMD as administrator
Sometimes our computer won’t let us perform certain actions without administrator permissions. Since this isn’t Unix, typing sudo won’t help at all. To run CMD as an administrator, right-click on the Command Prompt icon and choose Run as administrator.
Open CMD in the Explorer
To open CMD in any folder go to the desired location in Windows explorer and click on the top navigation bar.
Type cmd
and hit enter.
A new instance of CMD is going to open in that directory.
Exit CMD
To quit CMD simply type exit
and hit enter.
Congratulations if you’ve made it this far. We learned a lot in this introduction to CMD. In the next chapter, we will take a step further into networking, managing applications, and running system commands.