292

In terminal, how can I define a key to go to the previous directory which I was in when changing directory with the cd command?

For example, I'm in /opt/soft/bin and I cd into /etc/squid3 and I want to get back to the first directory.

Mat
  • 51,578
  • 10
  • 158
  • 140
Hojat Taheri
  • 5,026
  • 6
  • 23
  • 22
  • 1
    Does this answer your question? [What is the bash shortcut to change to the previous directory?](https://unix.stackexchange.com/questions/3284/what-is-the-bash-shortcut-to-change-to-the-previous-directory) – Peter Feb 28 '20 at 21:01

7 Answers7

411

You can use

cd -

or you could use

cd "$OLDPWD"
αғsнιη
  • 40,939
  • 15
  • 71
  • 114
Raphael Ahrens
  • 9,701
  • 5
  • 37
  • 52
  • 4
    Any idea why the directory name is printed to the console when using "cd -" ? – dtmland Aug 31 '15 at 23:56
  • 11
    Probably since the old shells didn't display the name of the current directory in the prompt, it was helpful to see the name of the directory when you typed `cd -`. For example when you type `cd /usr/local` you know that you are in `/usr/loal`, but when you type `cd -` you don't always remember from which directory you came from. So it saves you from typing `cd -; pwd`. But this is all speculation. – Raphael Ahrens Sep 01 '15 at 04:37
  • 3
    Or you can use aliasing: Set up alias as `alias -- -='cd -'` then use `-` (1 char) instead of `cd -` (4 char). Faster `:D` – ADTC Feb 16 '16 at 04:38
  • I used a simple technique instead. Just added a new line character after "`cd -`" so now `cd -\n` takes me directly to the next directory – Rajkiran Mar 10 '16 at 11:28
  • @ADTC : I added "Command + Left" key combination to go backwards in my iTerm Global Key Shortcuts. Faster and more related. – Rajkiran Mar 10 '16 at 11:32
  • @Rajkiran sure why not? But that's client-dependent. Look my comment on Atari911's answer, where I bound F12 to the command. Server-side and even faster (just one key). Though I agree, it's not related - but who uses F12 for anything anyway? – ADTC Mar 11 '16 at 07:35
  • 5
    How could I ever live without this? – VSZM Jun 07 '18 at 07:45
  • Is there a way to reverse this command and go to the previous directory, where I just came from (to the directory where I just typed `cd -`)? – André Kuhlmann Jan 06 '19 at 12:51
  • 1
    @AndréKuhlmann You can do `cd -` twice. Do `cd /; cd /usr; cd -; cd -` you should be in `/usr`. But maybe I miss understood your question. – Raphael Ahrens Jan 07 '19 at 19:42
  • @VSZM me too :) – linjiejun Mar 30 '21 at 01:18
  • Typing 'cd -' again is good way to do@AndréKuhlmann – linjiejun Mar 30 '21 at 01:19
63

The other answers are definitely complete in the direct answer sense. cd - and cd $OLDPWD are definitely the main choices for this. However, I often find that getting into a workflow with pushd and popd works better.

Long story short, if you are moving into a directory with the ultimate intent of coming back to where you started, use pushd/popd.

Extended example

The major difference is easily shown by an example.

$ cd dir1
$ pushd dir2

At this point, you have a directory stack that is dir2, dir1. Running pushd with no arguments will put you back in dir1 with the stack now as dir1, dir2. popd would do the same, but would leave you with an empty directory stack. This is not much different than how you would have been with the cd - workflow.

However, now you can now change directories multiple times and get back to dir1. For example,

$ cd dir1
$ pushd dir2
$ cd dir3

If you run popd at this point, you will go back to dir1.

Bernhard
  • 11,992
  • 4
  • 59
  • 69
Josh Berry
  • 861
  • 5
  • 4
  • 2
    You can also stack directories, so repeatedly use `pushd` and go back to previous folder while `popd`-ing. – Bernhard Jun 30 '13 at 19:08
  • 1
    Certainly. I almost put a large example that included checking the stack with `dirs -v`, but the reality for me is that often the simple case is all I really use. (Or, worse, I make a mistake when trying `push +2` or similar) Also, I thought baby steps for getting someone to try the workflow. :) – Josh Berry Jul 01 '13 at 02:53
  • How do you `cd` to the top of the stack without popping? – Jon Deaton Sep 11 '19 at 00:46
  • Do you need to use `popd` at all? Could you not just `cd` and `pushd` everywhere? Are there any disadvantages to doing so? – Hashim Aziz Nov 12 '19 at 16:35
  • @Prometheus, if just CD around you also don't need to push. The idea is to retrieve the position later with pop. – The Fool Aug 18 '20 at 15:27
  • https://stackoverflow.com/questions/5193048/bin-sh-pushd-not-found – Константин Ван Jun 25 '21 at 01:37
26

You should use:

cd ~-

it does the same as cd - (from the currently accepted answer) without the annoying echo of the directory and is easier to type than cd "$OLDPWD" or cd - > /dev/null.

Anthon
  • 78,313
  • 42
  • 165
  • 222
  • 2
    Redirecting the echo to null file ! It's genius !! – RamValli Feb 11 '16 at 06:19
  • 2
    The echo shows the new directory not the previous one. And I would find it useful, so that I know where I am now, especially if I'm in a shell that only shows the current directory name (or nothing at all) on the prompt, and I'm too unbothered to change the prompt to show the full path. Of course, if your prompt already shows the full path, the echo would be redundant and annoying. In that case, I think you can try doing `alias -- -='cd "$OLDPWD"'` then using `-` (1 char) instead of `cd ~-` (5 char). `:)` – ADTC Feb 16 '16 at 04:37
  • Source and explanation: https://unix.stackexchange.com/a/330885/38213 Spoilers, - is an operand and ~'s are path aliases. So `cd ~-/..` works but `cd -/..` does not! – Ray Foss Jun 07 '19 at 14:27
  • Why does this work? Where can I find the documentation? It’s not on the `--help`. – Константин Ван Jun 24 '21 at 06:56
19
$ cd - 

will change to the previous working directory.

mezi
  • 922
  • 1
  • 12
  • 17
3

You can "define a key" for cd - by editing your ~/.bashrc file and including an alias for the command. For example you could add cdc to make it cd - which would provide you with a shorter way to get to the last directory by adding:

alias cdc='cd -'

This way you would simply type cdc and it would put you in your last working directory.

Chris Tang
  • 121
  • 4
Atari911
  • 164
  • 4
  • 7
    And confuse you to no end when you use a system where that alias isn't in place. It saves typing one character every once in a while. Why even bother? – user Jul 01 '13 at 14:16
  • 2
    "In terminal, **how can I define a key** to go to the previous directory" I never get confused. Its just a shortcut, when you are on another system just use the long-hand way. – Atari911 Jul 01 '13 at 19:25
  • @Atari911 great question! here's the answer: http://stackoverflow.com/questions/4200800 Here's what I did (bound F12): `bind '"\e[24~":"\C-k \C-ucd -\n"'` (or more preferably move it to `inputrc` file as mentioned in the answer). – ADTC Feb 16 '16 at 05:00
1

cd .. goes to the precedent folder in the folder's tree.
cd - goes to the folder which it was before. This command didn't work on some distros (ubuntu 16.04), works in debian 9.

Scorpion
  • 737
  • 1
  • 7
  • 18
0

I find the combination of fzf and dirs to be powerful for quickly navigating to any previously visited directory. fzf helps me sift through my directory history as dirs helps me list the directories on the directory stack.

I define the following alias called ch as follows:

alias ch="cd \$(dirs -pl | fzf)"

Now, if you type ch in the terminal you will see the following, and you can select the directory you want to move into. enter image description here

Harsh Verma
  • 111
  • 3