How can I return to the previous directory in windows command prompt?
Asked Answered
B

8

25

I often want to return to the previous directory I was just in in cmd.exe, but windows does not have the "cd -" functionality of Unix. Also typing cd ../../.. is a lot of typing.

Is there a faster way to go up several directory levels?

And ideally return back afterwards?

Bighorn answered 10/1, 2018 at 14:47 Comment(2)
just type on the cmd cd ../Reportage
No that just moves up a directory, not latterly for example. Or to a different drive letter.Bighorn
B
6

Run cmd.exe using the /k switch and a starting batch file that invokes doskey to use an enhanced versions of the cd command.

Here is a simple batch file to change directories to the first parameter (%1) passed in, and to remember the initial directory by calling pushd %1.

md_autoruns.cmd:

@echo off
cd %1
pushd %1
title aliases active
cls
%SystemRoot%\System32\doskey.exe /macrofile=c:\tools\aliases

We will also need a small helper batch file to remember the directory changes and to ignore changes to the same directory:

mycd.bat:

@echo off
if '%*'=='' cd & exit /b
if '%*'=='-' (
    cd /d %OLDPWD%
    set OLDPWD="%cd%"
) else (
    cd /d %*
    if not errorlevel 1 set OLDPWD="%cd%"
)

And a small aliases file showing what to do to make it all work:

aliases:

cd=C:\tools\mycd.bat $*
cd\=c:\tools\mycd.bat ..
A:=c:\tools\mycd.bat A:
B:=c:\tools\mycd.bat B:
C:=c:\tools\mycd.bat C:
...
Z:=c:\tools\mycd.bat Z:
.=cd
..=c:\tools\mycd.bat ..
...=c:\tools\mycd.bat ..\..
....=c:\tools\mycd.bat ..\..\..
.....=c:\tools\mycd.bat ..\..\..\..
......=c:\tools\mycd.bat ..\..\..\..\..
.......=c:\tools\mycd.bat ..\..\..\..\..\..
........=c:\tools\mycd.bat ..\..\..\..\..\..\..
.........=c:\tools\mycd.bat ..\..\..\..\..\..\..\..
tools=c:\tools\mycd.bat C:\tools
wk=c:\tools\mycd.bat %WORKSPACE%

Now you can go up a directory level by typing ..

Add another . for each level you want to go up.

When you want to go back, type cd - and you will be back where you started.

Aliases to jump to directories like wk or tools (shown above) swiftly take you from location to location, are easy to create, and can really help if you work in the command line frequently.

Bighorn answered 10/1, 2018 at 14:47 Comment(1)
This is great. Also, if you want this to work in all of your command prompts just follow the answer: #17404665Mettlesome
O
22

Steps:

  1. pushd . (Keep old folder path on the stack)
  2. cd ..\.. (Move to the folder whare you like to)
  3. popd (Pop it from the stack. Meaning, Come back to the old folder)
Owens answered 19/2, 2021 at 19:49 Comment(1)
This works but.. seems only convenient in a script, not regular usage.Corridor
J
9

On Windows CMD, I got used to using pushd and popd. Before changing directory I use pushd . to put the current directory on the stack, and then I use cd to move elsewhere. You can run pushd as often as you like, each time the specified directory goes on the stack. You can then CD to whatever directory, or directories , that you want. It does not matter how many times you run CD. When ready to return , I use popd to return to whatever directory is on top of the stack. This is suitable for simple use cases and is handy, as long as you remember to push a directory on the stack before using CD.

Joint answered 10/1, 2018 at 17:9 Comment(1)
I was hoping for something built in, that didn't depend on me.Bighorn
L
9

This worked for me in powershell

cd ..
Lorenz answered 5/5, 2022 at 12:3 Comment(3)
This command moves to the parent directory. The OP is asking for a way to move to the previous directory, regardless of its location in the hierarchy.Guiltless
Read my comment for the answer...Guiltless
cd C:\Windows, cd C:\Users, cd .. leaves you at C: - OP is asking for a command that would take you back to C:\WindowsGuiltless
B
6

Run cmd.exe using the /k switch and a starting batch file that invokes doskey to use an enhanced versions of the cd command.

Here is a simple batch file to change directories to the first parameter (%1) passed in, and to remember the initial directory by calling pushd %1.

md_autoruns.cmd:

@echo off
cd %1
pushd %1
title aliases active
cls
%SystemRoot%\System32\doskey.exe /macrofile=c:\tools\aliases

We will also need a small helper batch file to remember the directory changes and to ignore changes to the same directory:

mycd.bat:

@echo off
if '%*'=='' cd & exit /b
if '%*'=='-' (
    cd /d %OLDPWD%
    set OLDPWD="%cd%"
) else (
    cd /d %*
    if not errorlevel 1 set OLDPWD="%cd%"
)

And a small aliases file showing what to do to make it all work:

aliases:

cd=C:\tools\mycd.bat $*
cd\=c:\tools\mycd.bat ..
A:=c:\tools\mycd.bat A:
B:=c:\tools\mycd.bat B:
C:=c:\tools\mycd.bat C:
...
Z:=c:\tools\mycd.bat Z:
.=cd
..=c:\tools\mycd.bat ..
...=c:\tools\mycd.bat ..\..
....=c:\tools\mycd.bat ..\..\..
.....=c:\tools\mycd.bat ..\..\..\..
......=c:\tools\mycd.bat ..\..\..\..\..
.......=c:\tools\mycd.bat ..\..\..\..\..\..
........=c:\tools\mycd.bat ..\..\..\..\..\..\..
.........=c:\tools\mycd.bat ..\..\..\..\..\..\..\..
tools=c:\tools\mycd.bat C:\tools
wk=c:\tools\mycd.bat %WORKSPACE%

Now you can go up a directory level by typing ..

Add another . for each level you want to go up.

When you want to go back, type cd - and you will be back where you started.

Aliases to jump to directories like wk or tools (shown above) swiftly take you from location to location, are easy to create, and can really help if you work in the command line frequently.

Bighorn answered 10/1, 2018 at 14:47 Comment(1)
This is great. Also, if you want this to work in all of your command prompts just follow the answer: #17404665Mettlesome
G
1

It can be solved by a hybrid doskey/batch-file implementation.
It replaces the built-in CD command with a DOSKEY macro.

@echo off
REM *** Trampoline jump for function calls of the form ex. "C:\:function:\..\MyBatchFile.bat"
FOR /F "tokens=3 delims=:" %%L in ("%~0") DO goto :%%L


doskey cd=for %%# in ( 1 1 2) do @if %%#==2 ( "%~d0\:__cd:\..\%~pnx0" ) else set args=$*

exit /b

:__cd

setlocal EnableDelayedExpansion

for /F "delims=" %%a in (""!args!"") DO (
  endlocal
  if "%%~a"=="" (
      if /i not "%CD%"=="%USERPROFILE%" (
          chdir /d "%USERPROFILE%" && set "OLDPWD=%CD%"
      ) 
  ) else if "%%~a"=="-" (
       if /i not "%CD%"=="%OLDPWD%" (
            chdir /d "%OLDPWD%" && set "OLDPWD=%CD%"
       ) 
  ) else (
        if /i not "%CD%"=="%%~a" (
            chdir /d %%~a && set "OLDPWD=%CD%"
        )
  )
)

Now it's possible to use

C:\> cd C:\Windows\System32
C:\Windows\System32> cd C:\users
C:\Users> cd -
C:\Windows\System32> cd -
C:\Users>

The source and more information at Sanitizing command parameters in a DOSKEY macro

Getupandgo answered 15/1 at 11:51 Comment(0)
B
0

If you go into File Explorer and right-click on a folder you would like to open in your command prompt, an option to "Open in Terminal" should pop up. If you hit that, it should open a terminal with the directory to the folder you would like to run a file from all ready for you!

Bergerac answered 4/4 at 19:2 Comment(2)
I can't see any relevance to the question.Getupandgo
The point was really operating from a command line.Bighorn
M
-4

cd .. works for me in ubuntu.

Check second most voted answer.

enter image description here

Mccrary answered 10/1, 2018 at 14:47 Comment(0)
D
-4

You could use the command:

cd ..\       -> To go up one level
cd ..\..\    -> To go up two levels

Note the space after cd

Discover answered 20/1, 2020 at 19:58 Comment(1)
This is not an answerMapes

© 2022 - 2024 — McMap. All rights reserved.