microsoft toolkit download

How To Change Directory In Command-Line or cmd?

Windows is not popular with its command-line interface currently but provides the command-line interface for some basic tasks. Windows command line can be used to navigate files and directories by changing the current working directory. The cd (Change Directory) command is used to change directories. There are also helper commands like popd and pushd which are used to change the directory.

Open Command Prompt (cmd)

First, we will launch or open the command prompt. The command prompt is also called MS-DOS or cmd.exe. Even it can be opened in different ways the most practical and easy way is to use the Start Menu by typing cmd or command prompt like below.

Open Command Prompt (cmd)

By default Open is used to open cmd with current user privileges but if you need Administrator privileges use Run as administrator button. But changing directories does not require Administrative privileges.

Default Working Directory of Command Prompt

When the command prompt is opened the default path or current working directory is the current user’s home path. The users home path is located under the Windows partition which is mostly C:\ the Users directory with the username. For this example the default directory is C:\Users\ismail .

Default Directory of Command Prompt

List Directories

Before changing the directory with the cd command listing directories for the current working or different path is very useful. The dir command is used to list files and folders easily like below. We can from the output that directories are labeled as <DIR> in order to specify it as a directory.

C:\Users\ismail> dir
 Volume in drive C has no label.
 Volume Serial Number is 801B-D913

 Directory of C:\Users\ismail

10/08/2020  03:26 PM    <DIR>          .
10/08/2020  03:26 PM    <DIR>          ..
08/26/2020  08:02 AM    <DIR>          .idlerc
08/26/2020  06:00 PM               178 .packettracer
10/06/2020  04:24 PM    <DIR>          3D Objects
07/06/2020  11:34 AM    <DIR>          Cisco Packet Tracer 7.3.0
10/06/2020  04:24 PM    <DIR>          Contacts
10/06/2020  04:24 PM    <DIR>          Desktop
10/06/2020  04:24 PM    <DIR>          Documents
10/08/2020  02:44 PM    <DIR>          Downloads
10/06/2020  04:24 PM    <DIR>          Favorites
10/06/2020  04:24 PM    <DIR>          Links
10/06/2020  04:24 PM    <DIR>          Music
09/10/2020  11:27 AM    <DIR>          OneDrive
10/06/2020  04:24 PM    <DIR>          Pictures
05/21/2020  10:36 AM    <DIR>          PycharmProjects
10/06/2020  04:24 PM    <DIR>          Saved Games
10/06/2020  04:24 PM    <DIR>          Searches
09/25/2020  02:40 PM    <DIR>          Test
10/06/2020  04:24 PM    <DIR>          Videos
10/08/2020  03:26 PM    <DIR>          wisetut
               1 File(s)            178 bytes
              20 Dir(s)  63,282,102,272 bytes free

We can also list different paths or directories’ contents by providing these paths like below. In the following example, we will list files and folders of the Desktop .

C:\Users\ismail> dir Desktop
 Volume in drive C has no label.
 Volume Serial Number is 801B-D913

 Directory of C:\Users\ismail\Desktop

10/06/2020  04:24 PM    <DIR>          .
10/06/2020  04:24 PM    <DIR>          ..
10/01/2020  03:51 PM    <DIR>          Backup
08/26/2020  08:02 AM                28 backup.py
07/10/2020  12:54 PM             3,442 bookmarks-2020-07-10.json
07/06/2020  11:33 AM             1,093 Cisco Packet Tracer.lnk
07/27/2020  01:23 PM             1,412 Opera Browser.lnk
09/23/2020  02:41 PM                73 runwise.bat
10/01/2020  03:51 PM    <DIR>          Test
08/26/2020  08:02 AM                28 test.py
               6 File(s)          6,076 bytes
               4 Dir(s)  63,286,165,504 bytes free

Alternatively, we can use an absolute path in order to list specified path contents, directories, and files. In the following example, we will list directories of the “C:\Users\ismail\Desktop“.

> dir C:\Users\ismail\Desktop

cd Command Syntax

The cd command is the most popular and defacto way to change directories. As a simple command cd provides very simple syntax and options to use.

cd OPTION DRIVE:DIRECTORY
  • OPTION is generally not used.
  • DRIVE is the drive letter of the path but it is optional and generally not used.
  • DIRECTORY is the directory name we want to change. DIRECTORY can be a relative or absolute path with single or multi-level directories.

Change Directory with Relative Path

Let’s start with a simple example where we will change the current working directory by providing the relative path. We will put the absolute path after the cd command as a parameter. If the path contains spaces double quotes can be used.

C:\>cd Users

C:\Users>cd ismail\Desktop

C:\Users\ismail\Desktop>

Change Directory with Absolute Path

An absolute path is the most reliable way to change the directory where the full path of the directory is provided. The absolute path also contains the drive letter or partition like C: , D: etc.

C:\>
C:\>cd c:\Users\ismail\Downloads

c:\Users\ismail\Downloads>
c:\Users\ismail\Downloads>cd c:\Users\ismail\Desktop

c:\Users\ismail\Desktop>
c:\Users\ismail\Desktop>cd "c:\Program Files"

c:\Program Files>

Change Directory To The Upper or Parent

The cd command also provides .. in order to specify one level upper directory or parent directory. Lets use the .. to change single or multiple times upper or parent directories.

c:\Users\ismail\Downloads\MyTools>
c:\Users\ismail\Downloads\MyTools>cd ..

c:\Users\ismail\Downloads>cd ../../..

c:\>

Change Directory To The Root Of Current Partition or Drive

The cd command provides the / paremeter which can be used to change the current working directory to the root of the current partition or drive. For example, if the current working directory is one of c:\Users\ismail\Downloads\ , c:\Users\ismail\ , c:\Users when the cd /d is used the current working directory will be changed to the C:\ .

c:\Users\ismail\Desktop>cd \

c:\>cd c:\Users\ismail

c:\Users\ismail>
c:\Users\ismail>cd \

c:\>

Change Partition or Drive

Windows may contain multiple drives or partitions with different letters. Generally the sequential letters like D: , E: , … are used. We can also navigate into these drives by using the cd command but if we specify the cd command like below it will not throw an error but also will not change the directory. cd D:\backup command will not work because changing partition or drive needs the /d parameter to work successfully.

C:\>
 
C:\>cd /d d:\backups

d:\backups

Alternatively just typing the drive or partition letter we want to navigate will change to the given partition like below. In the following example, we will type d: which can be also D: to change partition D: .

C:\>
 
C:\>d:

d:\

Using Tab Key Auto-Complete Path

The windows command line provides the ability called Auto Complation where the first letters of the path are typed by pressing Tab key will auto-complete the pathname. If there are multiple paths or directories where the typed letters are the same the first directory will be completed automatically but pressing the Tab key again will change to the second path with the same first letters etc. Let’s make an example. Type cd D and like below.

c:\Users\ismail>cd D

and press the Tab key which will result following.

c:\Users\ismail>cd Desktop

Now I will press the tab key again the change to the second alternative.

c:\Users\ismail>cd Documents

Display Current Working Directory

The current working directory can be displayed by printing the %CD% environment variable to the command line. The echo command can be used to print %CD% like below.

c:\>echo %cd%
c:\

c:\>cd Users\ismail\Downloads

c:\Users\ismail\Downloads>echo %cd%
c:\Users\ismail\Downloads

c:\Users\ismail\Downloads>

Change Directory By Storing Current Working Directory or Path with pushd Command

Windows command-line interface also provides the pushd command which can be used to change the current working directory by storing it. This will be useful when we want to return to the current working directory without remembering or typing it with the popd command. The pushd command can be used to store multiple paths and can be reverted back with the popd command.

c:\Users>

c:\Users>pushd ismail

c:\Users\ismail>pushd c:\WINDOWS\system32

c:\Windows\System32>popd

c:\Users\ismail>popd

c:\Users>

You can use the pushd and popd commands for different drives or partitions without a problem. Also, the pushd and popd commands are very useful for the UNC paths, etc.

Leave a Comment