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 would like to frequently switch between directories that are in totally unrelated paths, for example /Project/Warnest/docs/ and ~/Dropbox/Projects/ds/test/.

But I don't want to type cd /[full-path]/ all the time. Are there any shortcut commands to switch to previously worked directories?

One solution I could think of is to add environment variables to .profile for the frequently used directories and cd to them using those variables.

But is there any other solution to this?

share|improve this question
in office i am using Mac OS X 10.6, which has bash shell.. in home just recently installed UBUNTU and i am not sure what shell it has.. –  saiy2k Feb 8 '12 at 10:45
It has bash - run echo $SHELL. –  new123456 Feb 8 '12 at 13:23
Symbolic links could also be useful for this. en.wikipedia.org/wiki/… –  user606723 Feb 9 '12 at 15:11
add comment

21 Answers

If you're just switching between two directories, you can use cd - to go back and forth.

share|improve this answer
How this has eluded me for so long is a mystery. Thank you very much for this excellent tip. –  Mr. Shickadance Feb 9 '12 at 15:21
+1 Certainly one of my favorites and one that somehow many experts have 'missed'. –  Michael Durrant Apr 24 '12 at 2:17
add comment

If it's a small number of directories, you can use pushd to rotate between them:

# starting point
$ pwd
# add second dir and change to it
$ pushd ~/Dropbox/Projects/ds/test
~/Dropbox/Projects/ds/test /Project/Warnest/docs
# prove we're in the right place
$ pwd
# swap directories
$ pushd
/Project/Warnest/docs ~/Dropbox/Projects/ds/test

unlike cd -, you can use this with more than two directories

Following up on Noach's suggestion, I'm now using this:

function pd()
    if [[ $# -ge 1 ]];
        dirs -v
        echo -n "? "
        read choice
    if [[ -n $choice ]];
        declare -i cnum="$choice"
        if [[ $cnum != $choice ]];
        then #choice is not numeric
            choice=$(dirs -v | grep $choice | tail -1 | awk '{print $1}')
            if [[ -z $choice || $cnum != $choice ]];
                echo "$choice not found"
    pushd $choice

example usage:

# same as pushd +1
$ pd 1

# show a prompt, choose by number
$ pd
 0 ~/Dropbox/Projects/ds/test
 1 /Project/Warnest/docs
 2 /tmp
? 2
/tmp ~/Dropbox/Projects/ds/test /Project/Warnest/docs

# or choose by substring match
$ pd
 0 /tmp
 1 ~/Dropbox/Projects/ds/test
 2 /Project/Warnest/docs
? doc
/Project/Warnest/docs /tmp ~/Dropbox/Projects/ds/test

# substring without prompt
$ pd test
~/Dropbox/Projects/ds/test /Project/Warnest/docs /tmp

etc. Obviously this is just for rotating through the stack and doesn't handle adding new paths - maybe I should rename it.

share|improve this answer
Ooh, I knew about pushd and popd for traversal, but not that pushd could rotate what's been used so far... –  Izkata Feb 8 '12 at 20:29
add comment

There is a shell variable CDPATH in bash and ksh and cdpath in zsh:

CDPATH    The search path for the cd command. This is a colon-separated
          list of directories in which the shell looks for destination
          directories specified by the cd command.

So you can set in your ~/.bashrc:

export CDPATH=/Project/Warnest:~/Dropbox/Projects/ds

Then cd docs and cd test will take you to the first found such directory. (I mean, even if a directory with the same name will exist in the current directory, CDPATH will still be consulted. If CDPATH will contain more directories having subdirectories with the given name, the first one will be used.)

share|improve this answer
It should be mentioned that in general, you'll want the first entry in $CDPATH to be . (an explicitly entry, i.e. : also works). Otherwise you'll end up with the odd behavior where CDPATH dirs take precedence over directories in the current working directory, which you probably do not want. –  jw013 Feb 8 '12 at 23:40
I do pretty much the same thing, but without the export. That way, CDPATH is not exported to scripts with potentially weird or harmful effects. See here for more discussion and examples. –  Telemachus Feb 17 '12 at 12:47
"bash and ksh and cdpath in zsh" ... and in tcsh (just answering a question based on that over on Super User and found this while looking for similar answers on SE). –  Hennes Nov 13 '13 at 7:36
add comment

Something else you might try is a tool called autojump. It keeps a database of calls to it's alias (j by default) and attempts to make intelligent decisions about where you want to go. For example if you frequently type:

j ~/Pictures

You can use the following to get there in a pinch:

j Pic

It's installed system-wide, but included on a per-user basis in ~/.bashrc or ~/.zshrc by default.

share|improve this answer
Autojump is probably the best tool for this, it takes a little while to build up the database of common dirs, but once it does I think you'll find that you can't live without it. I know every time I'm on someone else's computer I feel crippled. –  quodlibetor Feb 13 '12 at 18:29
add comment

Try the cdable_vars shell option in bash. You switch it on with shopt -s cdable_vars.

Then you need to set your variables export dir1=/some/path. and finally cd dir1, etc. You can then put it in your ~/.bashrc to make it stick.

share|improve this answer
add comment

The following appeared to work on the one case I tested it on, and you can just drop your directory names as symlinks in ~/Bookmarks:

mkdir "$HOME/Bookmarks"
ln -s /tmp "$HOME/Bookmarks/testdir"

function ccd() { cd $(readlink "$HOME/Bookmarks/$1") ; }

ccd testdir && echo $PWD
# gives /tmp
share|improve this answer
that ccd() function need to typed in the terminal prompt or somewhere else? can u pls explain? –  saiy2k Feb 8 '12 at 8:41
@saiy2k: sorry, yes. The function line goes into your .bashrc (you can type it in your terminal to test, but it'll be gone when you close that window), the lines before set up the test case of "testdir" becoming a name for /tmp, and the last line is the test to see if it works. –  Ulrich Schwarz Feb 8 '12 at 17:19
add comment

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


share|improve this answer
The very same script was also published in the Linux Gazette, Issue #109, December 2004. –  on4aa Jun 20 '13 at 21:12
add comment

I use alias in bashrc to do those cds.
such as:

alias wdoc='cd ~/Project/Warnest/docs'
alias dstest='cd ~/Dropbox/Projects/ds/test'
share|improve this answer
is bashrc a file like .profile? in which I need to add those lines? –  saiy2k Feb 8 '12 at 7:43
~/.bashrc or /etc/bash.bashrc. I did not use .profile before, so don't know the relationship between them :-( –  Felix Yan Feb 8 '12 at 7:45
scripts bashrc will start everytime you open terminal. profile with startup. –  user14517 Feb 8 '12 at 10:42
added those lines to my .bash_profile and it works great.. thx :) –  saiy2k Feb 8 '12 at 10:56
add comment

I'd advice using zsh, that shell as very good TAB completion for directories, files, and even options for most cli programs.

I've been using that shell for years now, and I'd miss the functionality if it was gone. Scripting the zsh is a lot of fun, too, with a large number of one-liners that can help you every day.

share|improve this answer
No need to change to zsh for TAB completeion since bash has it all the same. For other functionality maybe but not for this. –  Peer Stritzinger Feb 9 '12 at 11:14
@PeerStritzinger Bash introduced that kind of functionality in BASH 4.0, but compared to zsh, it is still quite far behind. Saying "all the same" is certainly incorrect. –  polemon Feb 22 '12 at 0:02
Well zsh is ceartainly the übershell but just for tab completion there is no need to change (question was asked for bash). Besides bash 4.0 was introduced about 3 years ago ... –  Peer Stritzinger Feb 22 '12 at 9:45
add comment

There are a lot of good suggestions here. Which to use would depend on whether you have a small fixed list of directories you switch among, or whether you are looking for a more generic solution.

If it's a small fixed list, setting up simple aliases (as Felix Yan suggested) would be easiest to use.

If you're looking for a more generalized solution (i.e. many different directories, changing over time), I'd use pushd and popd (as Useless suggested). I personally find the default pushd/popd to be hard to use, especially as you start switching among many folders; however I wrote a few tweaks that make it much easier for me. Add the following to your bashrc:

alias dirs='dirs -v'
pd () 
    if [ "$1" ]; then
        pushd "${1/#[0-9]*/+$1}";
    fi > /dev/null
  • Use pd (as a shorter form of pushd) to jump to a new folder, remembering where you were.
  • Use dirs to see the list of saved directories.
  • Use pd 3 to jump to directory number 3.

Example Usage:

$ PS1='\w\$ '   ## just for demo purposes
~$ pd ~/Documents/data
~/Documents/data$ pd ../spec
~/Documents/spec$ pd ~/Dropbox/Public/
~/Dropbox/Public$ pd /tmp
/tmp$ pd /etc/defaults/
/etc/defaults$ dirs
 0  /etc/defaults
 1  /tmp
 2  ~/Dropbox/Public
 3  ~/Documents/spec
 4  ~/Documents/data
 5  ~
/etc/defaults$ pd 2
~/Dropbox/Public$ dirs
 0  ~/Dropbox/Public
 1  ~/Documents/spec
 2  ~/Documents/data
 3  ~
 4  /etc/defaults
 5  /tmp
~/Dropbox/Public$ pd 4
/etc/defaults$ dirs
 0  /etc/defaults
 1  /tmp
 2  ~/Dropbox/Public
 3  ~/Documents/spec
 4  ~/Documents/data
 5  ~
/etc/defaults$ pd 3
~/Documents/spec$ popd
~/Documents/data ~ /etc/defaults /tmp ~/Dropbox/Public
share|improve this answer
I see ... more pushd wrappers in my future :-) –  Useless Feb 8 '12 at 23:15
add comment

You could do worse than try j2.

From the README:

Spend a lot of time cd-ing around a complex directory tree?

j keeps track of where you’ve been and how much time you spend there, and provides a convenient way to jump to the directories you actually use.

I use it extensively & recommend it.

share|improve this answer
add comment

There's also OLDPWD, an environment variable which, according to IEEE 1003.1 (POSIX), should be updated with the previous working directory each time cd changes the working directory (for the curious ones, line 80244 of page 2506 of IEEE 1003.1-2008).

share|improve this answer
add comment

Use "pushd -n" (assuming you use bash).

Add to your ~/.bashrc:

pushd -n /Project/Warnest/docs/
pushd -n ~/Dropbox/Projects/ds/test/


cd ~ is your home,

cd ~1 is ~/Dropbox/Projects/ds/test/

cd ~2 is /Project/Warnest/docs/

You can use ~1,~2 etc in exactly the same way as ~.

share|improve this answer
add comment

There is a rather nice tool for quick directory changes:

xd - eXtra fast Directory changer http://xd-home.sourceforge.net/xdman.html

a bit awkward is that you need to map it in bash profile or similar as it only outputs the directory

# function to do `cd` using `xd`
# -g turns generalized directory search command processing on
# which improves the whole thing a bit
        cd `/usr/bin/xd -g $*`

you can do things like:

# change to /var/log/a* (gives you a list to choose from)    
f vla
# to skip the list and go directly to /var/log/apache2
f vlapach
share|improve this answer
add comment

if you're using zsh:

  • you don't have to type cd, just type directory path (/foo/bar/baz<Enter> equals to cd /foo/bar/baz<Enter>)

    requires auto_cd option to be set

  • you can expand abbreviated paths with Tab key (/u/sh/pi<Tab> expands to /usr/share/pixmaps; works for file names as well)
share|improve this answer
Bash 4 has gained shopt -s autocd, so it's no longer a zsh-only goodness. –  Gilles Feb 8 '12 at 23:44
add comment

There is also a "wcd" app created specifically for this (also ported to cygwin, since I am on that). You can create shortcuts, bookmarks of dirs with it. Also supports wild cards. Reading the man page & docs in /usr/share/wcd should help a lot.


share|improve this answer
add comment

Some suggestions here:

Most direct idea, I will add alias in the .profile file

vi ~/.profile
alias dir1='cd /myhome/onedir'
alias dir2='cd /jimmy/anotherdir'

Then use $ dir1 or dir2, can cd

If you are always switching in two dirs only. using cd - will switch between them.

share|improve this answer
add comment

working directory(wd) from atomic seems to be a good package for this: https://github.com/karlin/working-directory

share|improve this answer
add comment

cdargs is the most efficient tool for bookmarking a directory: http://www.youtube.com/watch?v=uWB2FIQlzZg

share|improve this answer
Is this a product you are associated with? –  Kazark Feb 27 '13 at 19:21
add comment

In my experience, the greatest speedup in navigating in a shell is to use its history search functionality. In Bash you can search backwards in your history of commands by pressing Ctrl+R and type in some pattern. That pattern is then matched against previous entries in your history -- may it be cd commands or other operations -- and suggestions are made as you type. Simply hit enter to run the suggested command again. This is called reverse-search-history in Bash and I love it. It saves me a lot of keystrokes and spares my internal memory.

It's a good thing because you only have to remember some smaller part of a command, like Drop or Wa to distinguish between the two history entries cd ~/Dropbox/Projects/ds/test/ and cd /Project/Warnest/docs/.

share|improve this answer
add comment

The solution I use for this situation is screen. Start screen and create a window for each directory with C-a c and navigate there. Change between windows/directories with C-a n or C-a p. Name the windows with C-a A. Then you can pop up a list of your windows with C-a " and navigate using the window number or name. Since it is screen, you can detach from the session saving your work space and re-attach later with the same set up.

share|improve this answer
add comment

Your Answer


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.