How can I get back to last used directory in a Linux shell?

5,422

Solution 1

cd -

cd - will bring you to the previously used directory (if there is one) or it will generate an error.

If a previously used directory exists it will change there updating the value of the current working directory and of the previous one, returning a successful exit status (0).
Otherwise it will print an error message and it will return an exit status (1).
Do your own check with cd -; echo $?.

The exit status will become important when you will use it in a script.
An exit status different from 0 can stop the execution of a whole script (if there is a set -e) or, worst, can lead to an exit point of the cd - command, different from the one you were thinking when you wrote the script and to execute commands in the wrong directory: imagine that you start in dir0; after you change to dir1 then you fail to change to dir2. Now you execute a cd -. You think to be in dir1 but instead you are in dir0... and from here it all in the hands of the Fate.


cd $OLDPWD (or cd $owd)

cd is an internal command of all the shells (all, starting from sh).
Underdash and bash it will set the variable PWD, OLDPWD for the present and old working directory. Under csh and tcsh it will set instead cwd and owd.

So with the command cd $OLDPWD in bash or cd $owd in tcsh you will be brought to the old working directory if it exists, or to your home directory if this variable is not set.
The exit code will be always 0, if you have access to your home directory(1).


pushd newdir ... popd

pushd adds a directory to the stack and popd removes one from the stack. An advantage respect cd - is that you will choose when to come back to the signed directory, and you will not be forced to come back to the last one. Another advantage is that you can pile a number of directories and decide to which to jump. help pushd and help popd for the information about those builtin commands.


Notes:

  1. To write cd $NotAlreadySetVariable is the equivalent to write cd with no parameters at all that will bring you to your home directory. To be precise it will bring you to the directory inside of $HOME (for bash,dash...) or $home (for csh,tcsh...). If this directory doesn't exist or is not accessible you will receive an error. If $HOME (or $home) is empty you will remain in the present directory, no error will be generated, and the $OLDPWD (or $owd) value will be set to the present directory.

  2. The $OLDPWD, $owd variable can be useful when you want to use the previous directory as parameter for a command. E.g. You want to move, all files from the present directory to the old one: mv * $OLDPWD.


Trivial
To be noted from man bash that in the definition of PWD is used current working directory(cwd) and not something like present working directory (PWD)...

PWD       The logical value of the current working directory.
          This is set by the cd command.

OLDPWD    The previous logical value of the current working directory.  
          This is set by the cd command.

HOME      The  home  directory of the current user; the default argument for  
          the cd builtin command. The value of this variable is also used when   
          performing tilde expansion.

from man tcsh

cd and pushd interpret `-' as the old working directory (equivalent to the shell variable owd). This is not a substitution at all, but an abbreviation recognized by only those commands. Nonetheless, it too can be prevented by quoting.

Solution 2

i figured out using cd command

cd - 

will redirect back to the last used directory

Solution 3

use pushd dir to change directory use popd to change dir back use dirs to view directories stack use pushd to switch current dir with previous

Share:
5,422

Related videos on Youtube

BlueBerry - Vignesh4303
Author by

BlueBerry - Vignesh4303

I love to wear redhat,bite some apples and throw it away through windows :)

Updated on September 18, 2022

Comments

  • BlueBerry - Vignesh4303
    BlueBerry - Vignesh4303 over 1 year

    How can I get back to the last directory used on a Linux shell?

    For example: I open a new shell (or a log in a different console shell) and I write :

    root@vignesh : cd /root/Desktop 
    

    This will direct me to the /root/Desktop directory (if I have access to it)

    root@vignesh:~/Desktop# 
    

    How can I now get back to the previous directory?
    Is it there any command to get back to the last directory I used?
    Did I need to again use the cd command to get back to previous directory?

    • Hastur
      Hastur over 8 years
      $OLDPWD store (when it exists) the path to the old directory used. To write cd - is the equivalent (quicker) to write cd $OLDPWD. You can figure out as you can have a wider use of $OLDPWD when you want to move, e.g., some files from the present directory to the old one mv * $OLDPWD etc etc...
    • BlueBerry - Vignesh4303
      BlueBerry - Vignesh4303 over 8 years
      @Hastur is pwd common stuff?while i wrote cd $NEWPWD it results the same
    • Hastur
      Hastur over 8 years
      You can see OLDPWD as the name of a system variable. When you write echo $OLDPWD it prints on the screen the value of that variable, in other words the old path in which you were. NEWPWD is an appropriate name for a variable but is not used by the system (I mean bash). pwd is both an internal command of bash and its duplicate executable in /bin/pwd, that prints the name of the current working directory. When you write cd $A_Not_set_VARIABLE is like if you write cd that bring you to your home and set the OLDPWD to the old path: it seems it didn't do anything... but it is not so :)
    • kostix
      kostix over 8 years
      Note that "linux terminal" is nonsense for two reasons: 1) the terminal is actually what enables you to input characters and see them printed; 2) cd is a command provided by the so-called "shell". The shell is what greets you with a prompt, provides for executing of commands, command history etc. And different shells provide different features, so you should have been provided us with what shell you're using. I do understand you're unfamiliar with these concepts so wanted to fuel your urge for research ;-)