One of the most basic, yet useful command line prompts is the ‘cd’ or “Change Directory” command.
As the name implies, this useful prompt allows you to change the current working directory.
The cd
command is like giving your computer a set of instructions on where to look for things in its memory.
Table of Contents
Open Command Prompt as Administrator
1) Press the Windows Key
2) Type cmd and press return
3) Right Click the Command Prompt icon and select ‘ Run as Administrator’
Whilst it isn’t entirely necessary to open the command prompt as an administrator for the ‘cd’ prompt it’s a good habit to open the Command Prompt as an Administrator regardless.
Type the cd Command
The cd
command is used in the command prompt to change the current working directory. The current working directory is the directory that the command prompt is currently “looking at”, and any commands you enter will be executed in the context of that directory.
Typing the command on it’s own won’t actually do anything (at least when you first open the command prompt and you are already in your “home” level);
cd
However when you enter the cd
command followed by a directory path, the command prompt will change the current working directory to the specified directory.
The directory path can be specified using an absolute path or a relative path.
Absolute path
An absolute path is the full path to a directory starting from the root of the file system. For example, C:\Users\Lewis\Documents
is an absolute path to the “Documents” folder on the C drive.
When you specify an absolute path with the cd
command, the command prompt will change the current working directory to the specified directory regardless of the current working directory.
In the above example you would type:
cd C:\Users\Lewis\Documents
Which would take you here;
Relative path
A relative path is a path to a directory relative to the current working directory. For example, if you are currently in the “C:\Users\Lewis” folder and you want to navigate to “Downloads” folder, you can just type cd Downloads
.
This is a relative path because it only specifies the location of the directory relative to the current working directory, not the full path.
In our above example, we are actually one folder level down, and therefore must “go back” a level first.
We can do this with the “cd ..” command, simply type;
cd ..
And you’ll be taken up one directory level
From there we could navigate to the Downloads folder, by simply typing;
cd downloads
cd command modifiers
The cd command doesn’t actually have any modifiers, at least not in Windows systems.
There are a couple for Unix-like systems, such as
-P
: this modifier makes thecd
command to change the physical directory instead of logical.-L
: this modifier makes thecd
command to change the logical directory instead of physical.-
: this modifier makes thecd
command to change to the previous working directory.
cd command arguments
The cd command does however have arguments, some of which we’ve covered in examples above.
Here are a few arguments that can be used with the cd
command in Windows:
cd [directory path]
: This argument is used to change the current working directory to the specified directory path. The directory path can be specified using an absolute or relative path.cd ..
: This argument is used to navigate to the parent directory of the current working directory. This command will take you up one directory level.cd %UserProfile%
: this argument takes you to your home directory on Windows, which is usually located atC:\Users\UserName\
.cd %HOMEPATH%
: this argument also takes you to your home directory on Windows.cd %SystemRoot%
: this argument takes you to the Windows system root directory, which is typically located atC:\Windows
.cd %ProgramFiles%
: this argument takes you to the “Program Files” directory, typically located atC:\Program Files
.cd %ProgramFiles(x86)%
: this argument takes you to the “Program Files (x86)” directory, typically located atC:\Program Files (x86)
.cd %UserProfile%\Desktop
: this argument takes you to the user’s desktop directory.
And a couple of Unix specific examples;
cd /
: this argument is not usable in Windows, this is only usable in Unix-like systems to navigate to the root directory.cd -
: this argument is not usable in Windows, this is only usable in Unix-like systems to navigate to the previous working directory.
In addition to these arguments, you can also use any valid directory path as an argument for the cd
command in Windows to navigate to that directory, pretty clever stuff right?
Summary
The cd
command is useful because it helps you navigate through the different folders and directories on your computer. Think of your computer like a big library, where all your files and programs are kept in different rooms (folders) and the cd
command is like a map that helps you find your way around the library.
Without the cd
command, you would have to remember the exact location of every file and folder on your computer and type out the full path every time you wanted to access it. With the cd
command, you can easily change to the directory where you need to be and access the files and folders stored there.
In short, the cd
command makes it easy for you to navigate through the different folders on your computer, which makes finding and accessing files and programs much more efficient.