Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Check this out superuser.com/a/449705/235569 –  Braiam Jul 25 '13 at 19:50
1  
@Braiam: The poster is looking to go to a previously-visited directory, not a directory above their current directory. –  Evan Teitelman Jul 25 '13 at 19:54
    
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
add comment

6 Answers

up vote 41 down vote accepted

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.
share|improve this answer
    
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. –  Evan Teitelman 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 4 '13 at 14:00
add comment

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.

share|improve this answer
4  
+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". –  Evan Teitelman Jul 26 '13 at 1: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
add comment

bash

I found a script 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

http://www.geocities.com/h2428/petar/bash_acd.htm

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
share|improve this answer
add comment

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.

share|improve this answer
    
thx, should have mentioned that I tried pushd in past but was really cumbersome. Just surprised this doesn't exist. –  timpone Jul 25 '13 at 20:01
    
@timpone: Maybe you could try aliasing cd to pushd. Some suggestions on this are discussed in this question. –  Thomas Nyman Jul 25 '13 at 20:09
    
thx, upvoted for response –  timpone Jul 25 '13 at 20:19
add comment

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)

share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.