Stupid cmd.exe tricks: Changing directories with forward slashes instead of backslashes


I observed one of my colleagues perform this trick with cmd.exe: You can pass a path with forward slashes (instead of backslashes) to the cd command, provided the first character is not a slash.

cd relative/path       &rem okay
cd ../relative/path    &rem okay
cd /absolute/path      &rem not okay
cd c:/absolute/path    &rem okay

It's not much of a trick, but it comes handy once in a while.

Comments (31)

  1. Daniel W says:

    When going back and forth with a user that is in Linux a lot, it’s handy that the OS can figure out what you intend to do.

    1. Rick says:

      Using forward slashes is very handy when running commands in cygwin that may spawn a child process outside of cygwin and pass a path parameter to the child. “c:/absolute/path” is the only syntax I find works consistently both inside and outside of cygwin-land.

    2. Joshua says:

      I’ve been using it for copy/paste from the output of git status. I was surprised when cd .. worked afterwords.

  2. florian says:

    One of my programs accepts directories with forward slashes passed on the command line, and I was thinking of a –file command line switch to make it explicit that /temp means \temp and not –temp.

  3. KeyJ says:

    But that’s not a specific cmd.exe feature, or is it? Even DOS treated forward and backward slashes synonymously in its API functions, it’s just that backward slashes are the canonical notation.

    1. The trick is getting it past the command line parser of whatever program you are using.

      1. Brian_EE says:

        Most modern-day command line programs use ‘-‘ character to delineate parameters. At least the ones I tend to use.

        1. hyperman_ says:

          Even better, POSIX standardizes an opt-out: everything after 2 hyphens should be treated as a file: So ls -l lists in long format but ls — -l list the file named -l. This greatly enhances the stability of shell scripts ( the equivalent of .bat files on unixlikes)

          1. Joshua says:

            Shell scripts aren’t safe against untrusted filenames. What will you do when I put newlines in file names?

            There’s talk of banning all chars < 32 but no action yet.

          2. hyperman_ says:

            @Joshua Perfectly securing a shell script is hard and I’m not claiming I can do it. But I tested your example on debian/bash, and it works as intended. However, we are going very far off-topic, so this is my last post. Feel free to prove me wrong ;-)

            NOTE: Create a script that lists file info, first arg is a file name
            $ cat script.sh
            #!/bin/sh
            ls -- "$1" 
            
            NOTE: Create a file name consisting of 2 valid flags, separated by a newline
            $ touch -- "-l
            -a"
            
            NOTE: Execute the script
            $ ./script.sh "-l
            > -a"
            NOTE:  Next line is the output.  You see the file name, and the newline char has been masked by a question mark.
            -l?-a
            
            NOTE: This is an example of what happens if -l and -a are flags; the failure case
            $ ls -l -a -- "-l
            > -a"
            -rw-r--r-- 1 username group 0 jun  8 21:21 -l?-a
            
    2. Rick says:

      It’s a kernel feature, but all of the shell utilities (like cmd.exe) need to not freak out or misinterpret when given a forward slash.

  4. The MAZZTer says:

    Works in the Run dialog too, at least on Windows 7.

    Many third-party cross-platform apps use forward slashes since Windows is the only platform that uses backslashes. I was never sure if they converted the slashes internally or if Windows could handle them.

    It probably depends on context, since / is traditionally used in DOS/Windows for command line switches so it can’t be used when it could possibly refer to that…. which would explain why Raymond’s third example doesn’t work!

    1. ender9 says:

      Win32 API handles forward slashes internally (however, it only works with regular paths – if you use \\?\, you have to use backslashes).

    2. Cat Mara says:

      One of my favourite but least well-known features of the Run dialog and the Explorer address bar is that they both understand environment variables so that you can, for example, type %TEMP% into them and be taken to your temporary directory

      1. hyperman_ says:

        It also works in file browsers and the open/save dialog (at least on win7)

  5. IanBoyd says:

    I specify all my paths using pidls. That way i can cd into my phone.

    1. sba says:

      Can you elaborate a little more? I’d be interested in accessing my (Lumia 950) phone from scripts…. do you think that PIDL trick would fit the bill?

  6. anai says:

    Re: #3 “cd /absolute/path”

    cd /windows/temp

    …works fine for me..?..(win7)

  7. Kai Schätzl says:

    It’s sometimes handy when the keyboard doesn’t fit your language.

    1. Alex Cohn says:

      … or when backslash is displayed as ₩

  8. Roger says:

    It’s handy when pasting the output of various git commands, but general usage in the console is a crap-shoot. For instance you’ll lose most aspects of tab-completion when using the / … :-/

  9. Edward Ellis says:

    The downside is that with forward slashes tab completion no longer works.

  10. BOFH says:

    cd /absolute/path &rem not okay

    However:
    cd “/absolute/path” &rem okay

  11. Keith says:

    not sure if I enabled something to do this ..

    cd /absolute/path

    works fine for me?

  12. Vince Valenti says:

    Something must have changed recently. I am running Windows 1803 and I am able to do things such as:

    cd /
    cd /Users/Vince

    But, if I have a root directory named “D”, I cannot cd /D. :)

  13. alegr1 says:

    Forward slashes are not handled in:

    1. cmd.exe TAB handler for auto-completion – a path entered with slashes doesn’t get autocompletions
    2. File open dialog: If you enter/copy a file path with backslashes it gets opened OK, but not with forward slashes

    1. John Elliott says:

      Forward slashes worked in the Windows 3.x file open dialog, but broke in (IIRC) Windows 95. Possibly related to the ability to enter a URL (which of course would have had forward slashes) in that field.

  14. dspig says:

    Great, now I just need cd.. (with no space) to work on Linux :)

    1. Joshua says:

      alias cd..=’cd ..’

  15. CompuChip says:

    The one I surprise my co-workers with all the time is that you can type “cmd” in the file explorer window and it will open a command window in the same context as is currently displayed – no more need for the right-click command prompt here tool :)

    1. mortb says:

      Nice tip!

Skip to main content