66

I often use cd - to go back to where I was. How can I do this multiple times in bash? Or would zsh or some other tool support this?

timpone
  • 763
  • Check this out http://superuser.com/a/449705/235569 – Braiam Jul 25 '13 at 19:50
  • 2
    @Braiam: The poster is looking to go to a previously-visited directory, not a directory above their current directory. –  Jul 25 '13 at 19:54
  • 1
    Since I can't edit, I've used pushd but find it basically unusable since it requires you to do all of this work ahead of time (basically you have to manage it). I'm REALLY surprised what I want to do is not supported; it is 2013 – timpone Jul 25 '13 at 20:00
  • Check your notifications, there is a edit on queue, you can cancel that and edit your question. – Braiam Jul 25 '13 at 20:06

6 Answers6

56

In zsh, there's an auto_pushd option. This option makes cd behave like pushd. Then you can just use popd to go back to previous directories.

~ $ setopt auto_pushd
~ $ cd /
/ $ cd /var
/var $ cd /usr
/usr $ dirs
/usr /var / ~
/usr $ popd
/var $ popd
/ $ popd
~ $

In Bash, you can alias cd to pushd.

alias cd=pushd

The one downside of this is that you will lose cd's three flags. From the cd help entry:

-L force symbolic links to be followed
-P use the physical directory structure without following symbolic links
-e if the -P option is supplied, and the current working directory cannot be determined successfully, exit with a non-zero status

If you ever have to use the actual cd builtin instead of the alias, you can use one of these:

  • 'cd' - Quoting the command makes the shell not resolve the alias and use the normal cd.
  • \cd - Backslashes quote characters. If you quote one character of a word, the shell treats the whole word as quoted.
  • builtin cd - This directly tells the shell to use the builtin instead of the alias.
  • cool, is there a way to do this in bash or just zsh? – timpone Jul 25 '13 at 20:04
  • @timpone: See the edit to my answer. –  Jul 25 '13 at 20:11
  • 1
    thx for pointing out tradeoffs. – timpone Jul 25 '13 at 20:21
  • Regarding the bash solution: there are more tradeoffs to be considered. E.g., I would too often write cd, meaning cd ~, and getting the swap of the two dirs at the top of the stack. I prefer simply using pushd when I need it. But I always take one alias with me: alias ad=pushd (ad stands for alternate directory) – Walter Tross Aug 04 '13 at 14:00
25

Here's a quick and dirty way to conveniently bookmark directories and jump back to them:

$ a() { alias $1="cd $PWD"; }

Go somewhere and type:

$ a 1

You now have a command called 1 which does cd the directory that is current at this time. Later just type:

$ 1

And presto, back to that directory. I find this very useful.

  • 7
    +1. This is an excellent idea. It will break if your current working directory has a space in it somewhere though. To fix that, you should put $PWD in double quotation marks: "$PWD". –  Jul 26 '13 at 01:39
  • Now all you need is to make it keep a list of the bookmarks, so you can have a command to remove them (so that you don't accidentally delete non-bookmark aliases) or clear all of them. – AJMansfield Jul 26 '13 at 16:37
20

bash

I found a script, available here, that solved this issue for me. With this you can type cd -- to see the last 10 directories that you've used. It'll look something like this:

0  ~/Documents/onedir
1  ~/Music/anotherdir
2  ~/Music/thirddir
3  ~/etc/etc

To go to ~/Music/thirddir just type cd -2

zsh

oh-my-zsh provides some really nice functionality for this (at least I think it's oh-my-zsh that sets this).

Basically, d is aliased to dirs -v | head -10

dirs is a zsh built-in command and shows the last directories you've been to.

Also, 1 is aliased to cd -1 and so on for all numbers to 9.

In practice, it works like this:

$ pwd
/home/me/Documents/gems/java_regex/lib

$ d 0 ~/Documents/gems/java_regex/lib 1 ~/Documents/gems/java_regex 2 ~/Documents/gems 3 ~/Documents 4 ~

$ 2 ~/Documents/gems

$ pwd /home/me/Documents/gems

Dean
  • 541
  • 2
    acd_func can be found at: https://github.com/djoot/all-bash-history/blob/master/acd_func.sh – jo_ Apr 05 '17 at 07:57
  • 1
    Archived link for the geocities link: https://web.archive.org/web/20091025214940/http://www.geocities.com/h2428/petar/bash_acd.htm – Pat Myron Dec 05 '17 at 21:43
6

You can use the pushd builtin in bash to push new working directories onto the directory stack. Use popd to pop directories of the stack in order to return to the previous working directories.

Thomas Nyman
  • 30,502
0

Yes. We can make your cd command more powerful. Bash profile allows us to customize it the way we want.

Rather than doing multiple cd to navigate up the directory, we can use alias

For example :

alias ..5="cd ../../../../.."

This will take you 5 directory up the level.

manatwork
  • 31,277
  • 1
    Question is "How can I hit the back button" on where I've been (Previous locations) - not "How can I go up a directory (or 5)". – WernerCD Jul 26 '13 at 17:12
0

I'm using simple function found on commandlinefu long time ago

 up() { local x='';for i in $(seq ${1:-1});do x="$x../"; done;cd $x; }

(added in my osx .profile works like a charm)

Kris
  • 101