Windows命令提示符返回原目录及快速跳转多级目录的方法问询
Solutions for Quick Directory Jumps & Returning to Original Path in CMD
Great question—this is a super common frustration when moving between Unix shells and Windows CMD. Let’s break down the most practical, native, and customizable solutions to solve both your problems:
1. Use pushd and popd (Native, No Setup Needed)
This is Windows’ built-in answer to Unix-style directory stacking, and it’s perfect for jumping around while keeping your original path accessible:
- Save your current directory and jump elsewhere: If you’re in a deep path like
C:\Users\Greg\Dev\WebApp\frontend\srcand need to switch toC:\Temp, run:
This pushes your current directory onto a hidden stack, then switches to the new path instantly.pushd C:\Temp - Return to your original directory in one step: Whenever you’re ready to go back, just run:
This pulls the last saved directory from the stack and switches back—no typing out long paths or repetitivepopd../../..! - Bonus: Jump multiple parent directories and save your spot: If you just need to jump up 3 levels (instead of a new path), run
pushd ..\..\..—thenpopdwill bring you right back to where you started.
2. Simulate Unix’s cd - with Custom Aliases
If you miss the cd - shortcut to toggle between your current and previous directory, you can set up a custom alias using doskey:
- Temporary alias (current CMD window only): Run these two commands in your prompt:
Now, every time you usedoskey cd=set OLDPWD=%CD% $T cd $* doskey cd-=cd "%OLDPWD%"cdto switch directories, it saves your old path to theOLDPWDvariable. Typecd-(you can adjust the alias tocd -if you prefer, just tweak the doskey command) to jump back to your previous directory. - Permanent alias: To make this stick across all CMD windows, add the commands to a
.batfile (e.g.,cmd_aliases.bat) and set it to run automatically when CMD starts—you can do this via the registry or by adding it to your user’sStartupfolder.
3. Quick Multi-Level Parent Directory Jumps
Typing ../../.. gets tedious fast—create a custom up command to jump N levels in one go:
- Temporary setup: Run this in CMD:
Now,doskey up=for /l %%i in (1,1,$1) do cd ..up 2jumps 2 levels up,up 4jumps 4 levels, etc. - Permanent setup: Add this
doskeycommand to your startup.batfile alongside thecd-alias for a fully customized CMD experience.
内容的提问来源于stack exchange,提问作者Gregory Bishop




