How To Change Directory In CMD (MS-DOS and PowerShell)? – POFTUT

How To Change Directory In CMD (MS-DOS and PowerShell)?

cmd or cmd.exe or MS-DOS is a command-line tool used in Windows operating systems. One of the most basic operations for the command line is changing directory. We can change the current working directory with the cd command. It may seem very basic and easy but a changing directory with the cd command provides a lot of different use cases like the change to a parent directory, change the drive, change to the root directory. In this tutorial, we will learn all of them.

cd Command Syntax

cd command has very simple syntax where we provide the path. There is also drive part but it is used very rarely and optionally. We can also use  chdir command like cdcommand which is the short form of chdir.

  • DRIVE is optional and specifies the drive or partition like C: , D: we can also use lowercase like c:, d:. DRIVE is generally not used.
  • PATH is the most important part where the directory names, . and .. are used to specify the path.

cd Command Help

As cd is very simple command it has also very little help information for complete usage. We can print help information with /? like below.

cd Command Help

cd Command Help

Change Directory

We will start with a simple usage where we will provide the directory we want to change. The given directory is a child directory of the current or current working directory. In this example, we will change to the directory named Users, ismail , Desktop in a row.

LEARN MORE  How To Compress Files and Folders In File System Level With Compact Command In Windows NTFS?

Change Directory

Change Directory

Using Tab To Complete Directory Name

MS-DOS provides tab shortcut to complete directory. If we do not know the whole directory name or it is too long to type we can use TAB key to list available directories. The completion will be done according to the provided directory name. For example, if we typed U and press TAB the directories like User, Users, Use will be listed. If we do not type any directory name all currently existing directories will be enumerated alphabetically.

Change Multiple Level/Directories

In the previous example, we have changed the directories one by one with multiple  cd commands. We can also specify multiple directories in a single cd command. In this example, we will change to the Users, ismail, Desktop.

Change To Directory Name with Spaces

Directory names can be upper case, lower case and provide some special characters like space. While navigating between directories with spaces we have to take care of the directory name. In order to prevent errors, we can use a double quote for the directory name with spaces or for the whole path like below. For example, in order to change directory to the Program Files we should use "Program Files" like below.

Change To Directory Name with Spaces

Change To Directory Name with Spaces

Change To Upper/Parent Directory

In order to change one level upper or parent directory typing, the whole path is not feasible. We can use .. double dot which simply references one level upper or parent directory.

LEARN MORE  Creating File Systems In Linux With mkfs

Change To Upper/Parent Directory

Change To Upper/Parent Directory

Change To Two Level Upper/Parent Directory

We can also use .. to change multiple levels of upper directories. In this example, we will go two-level upper directory with multiple  ... We will also use / in order to specify the directory level.

Change To Sibling Directory

We can change to the sibling directory. We will use .. to go one level upper directory and provide the sibling directory name like Downloads.

Change To Sibling Directory

Change To Sibling Directory

Change To Root Directory

The root directory is the highest level of the directory for the current partition or drive. We can directly change to the root directory with the cd\ command without providing extra information.

Change To Root Directory

Change To Root Directory

Print/Display Current Working Directory

While working and changing directories we may be lost in the paths. In some cases, we may need to print the current directory. The current working directory is the path we are currently working and can be printed with the %cd% environment variable like below. We will also use echo command to print the current working directory environment variable to the cmd.

Print/Display Current Working Directory

Print/Display Current Working Directory

Change Drive

Windows operating system generally used with multiple partitions or drives. While working with the  cd command navigation between these drives may be required. We can change the current drive or partition just by providing the destination drive or partition name like d: , e: etc. In the following example, we will change the current working partition to the D.

Change Drive and Directory

We can also change the current working partition and directory in a single and same command. WEe just need to provide the partition/drive name with the path or directory we want to change. In this example, we will change to the D partition Backups\2019.

Change Directory By Saving Current Path

Navigating the directories in the command line is a very trivial task if there are a lot of different directories to change. pushdcommand can be used to change a new directory by saving the current path to memory. The saved path can be used with the popdcommand which is explained below.

LEARN MORE  Useful Linux Commands A System Administrator Should Know

Change Directory To The Saved Path

In the previous example, we have saved the paths and these paths can be easy with the popd command to navigate.

Change Directory To The Saved Path

Change Directory To The Saved Path

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)