cd/chdir - Changing Directories
When using an explorer to manipulate files, it is common to navigate to the target folder before performing file operations.
Similarly, in the command prompt, there is the concept of the “current directory.” By navigating to the target directory, you can efficiently perform file operations.
This article explains the cd command for changing directories in the command prompt, from basic usage to setting options, in an easy-to-understand manner.
What is the cd Command?
The cd command stands for Change Directory. By using this command, you can change the current directory in the command prompt.
You can achieve the same result by typing chdir.
The basic usage of the cd command is as follows:
chdir [directory path]
cd [directory path]
For more detailed usage, you can specify the following options:
chdir [/d] [drive:][directory path]
cd [/d] [drive:][directory path]
Display the Current Directory
You can display the current directory by executing the cd command without any arguments.
cd
The output will be as follows:
Move to a Subdirectory
You can move to a directory by specifying the directory path with the cd command.
cd [directory path]
For example, to move from the C:\users\user directory to the Documents directory, you would write:
cd Documents
The result will be as follows:

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Move to the Parent Directory
You can move to the parent directory by specifying .. with the cd command.
cd ..
The result of the execution will be as follows:
Move to a Path Containing Spaces
With command extensions enabled (the default), cd accepts paths containing spaces as they are.
cd My Projects
cd "My Projects"
Both mean the same, but quotes are required when command extensions are disabled or the path contains special characters such as &. Always using quotes is the safe choice.
Specify an Absolute Path
You can move to a directory by specifying an absolute path with the cd command.
cd [drive:][directory path]
For example, to move from the C:\users\user directory to a completely different directory, C:\Windows, you would write:
cd C:\Windows
The result of executing this command will be as follows:
/d option - Change Drive
If your computer has a DVD drive or USB memory connected, you need to specify the /d option to move to that drive.
If you specify a path on another drive without the /d option, the current drive is not switched (only the current directory remembered for that drive is changed), so it looks as if nothing happened.
cd /d [drive:][directory path]
For example, to move from the C:\users\user directory to the root directory of the D: drive, you would write:
cd /d D:\
The execution result will be as follows:
When moving between drives, you can use the /d option of the cd command, or you can specify the drive name like D: to move.
D: Move to the Folder of the Batch File
In a batch file, the current directory at run time is not necessarily the folder containing the batch file (for example, when started from a shortcut or Task Scheduler). To move to the folder where the batch file itself is located, use %~dp0.
cd /d "%~dp0"
%~dp0 expands to the drive and folder path of the batch file. With /d, the move works even if the batch file is run from another drive.
cd cannot set a UNC path such as \\server\share as the current directory. Use the pushd command instead, which assigns a temporary drive letter and moves there.
When the specified directory does not exist
If the directory specified with the cd command does not exist, an error message will be displayed, and the change will not occur.
cd [nonexistent directory path]
The execution result will be as follows:
Summary
In this article, we explained how to use the cd command to navigate directories in the command prompt.
The cd command has various uses, from basic usage to setting options. Please try it out on the command prompt.