Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to a subdirectory in CMD?

Tags:

windows

cmd

How can I go to a subdirectory without specifying its whole path?

So when I am in

C:/users/USERNAME/desktop/project/build/

How can I then navigate to

C:/users/USERNAME/desktop/project/build/directory 1/

I tried

cd /directory 1

But this results in

The system cannot find the path specified.


I want to use something as simple as the .. command, which goes one directory back without specifying its whole path. Is there something similar to change to a subdirectory?

like image 864
Red Avatar asked Apr 21 '17 08:04

Red


1 Answers

On Windows the directory separator is \ ... the backslash character. The slash character / is used on Windows for parameters/options of a command/executable/script.

For compatibility reasons like #include instructions in C/C++/C# with a relative path using / as directory separator Windows accepts also paths with / and automatically convert them to \ when a string is interpreted as file/folder name with a path. That is done by the file IO functions used by Windows executables on accessing the file system of a storage device.

A path starting with a backslash is a path relative to root of current drive.

For that reasons cd /Directory 1 is executed as cd /D irectory 1 which means with interpreting /D at beginning of the directory argument string as optional option /D by command CD to change also the drive and the rest of the directory argument string not enclosed in double quotes as name of the subdirectory to change current directory to. The subdirectory irectory 1 does not exist on your machine.

If the directory name starts with any other character than letter D and using / instead of \ at beginning of the directory argument string like on using cd /Folder 1, there would be executed cd "C:\Folder 1" with drive C: being the current drive.

There are three types of relative paths:

  1. Path is relative to current directory if path starts with .\ or with directory name.
  2. Path is relative to parent directory of current directory if path starts with ..\.
  3. Path is relative to root of current drive if path starts with \.

.\ and ..\ can be also used anywhere inside a path after a directory separator multiple times and Windows automatically resolves the path to an absolute path.

So correct would be using cd "Directory 1", or not recommended cd Directory 1.

The help of command CD output on running cd /? explains that the command CD does not require enclosing a path with one or more spaces in double quotes as usually required because a space is usually interpreted as separator between arguments to pass to a command, executable, or script.

It is nevertheless best practice to always enclose the path in double quotes because of some other characters require path being enclosed in double quotes to interpret (nearly) all characters in path as literal characters.

For example a directory has the name Test & Doc. With cd Test & Doc the Windows command processor executes cd Test and next tries to find an executable or script with Doc and Doc.* having a file extension listed in environment variable PATHEXT in current directory or any directory listed in environment variable PATH because of the ampersand is interpreted as unconditional AND operator for execution of multiple commands on a single command line. But on using cd "Test & Doc" the entire double quoted string is interpreted as name of a directory to change to by the Windows command processor.

BTW: All internal (included in cmd.exe ... the Windows command processor) and external commands (executables installed by default in %SystemRoot%\System32) can be started with the parameter /? in a command prompt window to get displayed the help for this command.

  • cd /?
  • cmd /?
  • pushd /?
  • popd /?

More details on Windows commands can be found at

  • Microsoft's command-line reference
  • SS64.com - A-Z index of the Windows CMD command line
  • Single line with multiple commands using Windows batch file

The Microsoft documentation about Naming Files, Paths, and Namespaces offers even more details about file names and how paths are interpreted by Windows kernel. Everybody writing program or script code for applications or scripts executed on Windows should have read this documentation page at least once from top to bottom.

like image 153
Mofi Avatar answered Oct 18 '22 20:10

Mofi