16

Possible Duplicate:
Can windows command line support Linux “cd -”?

Under Linux, I can use cd - to return to the last directory. How can I do that on Windows?

Jichao
  • 7,145
  • 12
  • 52
  • 62

2 Answers2

24

You can use the pushd and popd commands.

  • pushd <dir> will change directory from location a to location b
  • popd will change directory back to directory a

Example:

pushd %TEMP%     // go to user's temp dir, and remember
pushd \Windows   // go to windows dir, and remember
popd             // go back one dir, in this case the temp dir
popd             // go back one more dir, in this where you were before temp
Abel
  • 1,421
  • 5
  • 18
  • 38
Xetius
  • 1,221
  • 1
  • 10
  • 15
  • 1
    It doesn't work for me, but using `cd..` works, thanks to @Акула (_[below answer](https://superuser.com/a/84948/643505)_) – Mehdi Dehghani Jul 06 '17 at 06:14
  • `cd ..` goes one directory up. `pushd/popd` can be used to remember and go back the previous directory that you were at. It is often used in batch files where you want to remember the directory the user was at before starting the batch file, so you can return to it when you finish. – Abel Jul 25 '20 at 23:52
6

Cmd.exe is an emulation layer for the old MS-DOS, commands are the same :

  1. One step backward = cd..
  2. All Backward = cd /

For the others look at some Ms-Dos table around the web

Акула
  • 309
  • 1
  • 3
  • 5
    This is not what cd - does. If one is in /a/b/c/d then cd's to /a/e/f, `cd -` as a command returns you to /a/b/c/d, whereas `cd ..` moves you to /a/e and `cd /` moves you to /. – Neal Dec 18 '09 at 15:18
  • 1
    You are right i've misunderstanded the question. (What poor figure!) :-( – Акула Dec 18 '09 at 15:39
  • please remove this . cd .. goes up one level in folder hierarchy and not *backwards* in history. Also on win/dos the folder notation is backward slash (\\) not forward slash (/) – elig Jul 07 '20 at 22:45
  • @elig, you can use either backward or forward slash nowadays, so `cd /` is valid. Just not when you use it with `pushd`, for some odd reason. But I agree it is not an answer to the question. – Abel Jul 26 '20 at 00:03