Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What are some good Linux commands to make switching between different directories on the command line easier?

I often have to switch between different directories with long names, e.g.

cd foo/20100801-rev124-test2
cd ../20100801-rev124-test1
cd ../20100802-rev126-bar

Typically, I use tab completion, so it's something like this:

cd ../2010080<TAB>2<TAB>-rev12<TAB>6-<TAB>bar

But it's annoying because all these names are so similar, so tab completion doesn't really save much work.

What tricks do you use to minimize typing in such situations?

BTW, I do use pushd and popd sometimes, but here it won't help much because I don't visit the directories in a given order.


For a nice solution, see the accepted answer to my other, related question. Basically, just create an alternative cd command:

function cd2 { select a in $1*; do cd $a; break; done }

It will list all possible completions at once:

$ cd2 2010080
(1) 20100801-rev124-test2
(2) 20100801-rev124-test1
(3) 20100802-rev126-bar

and you can pick one by typing its number.

share|improve this question

migrated from Aug 4 '10 at 16:16

This question came from our site for professional and enthusiast programmers.

Hmm, unless the solution requires coming up with a bash script or similar. – dehmann Aug 4 '10 at 15:51
use a good file manager with bookmarks and terminal integration. Also alias ".." to go to parent dir so you don't have to type "cd ..". And create symlinks to save typing. – neoneye Aug 4 '10 at 15:54
@neoneye: A file manager with bookmarks and terminal integration would be nice. I can't find any; do you have a link? – dehmann Aug 4 '10 at 16:02
I use mac.. however some file managers that is available for linux that most likely can do it: Dolphin, Worker. For textmode there is Midnight Commander. – neoneye Aug 4 '10 at 16:20

10 Answers 10

up vote 3 down vote accepted

The dirs -v command will list the directory stack with numbers in front of each directory, and you can give a number argument to the pushd command to have it jump to that directory. For example, if dirs -v shows that the directory you wish to change to has 2 in front of it (i.e., is the third directory in the list), executing pushd +2 will change to that directory.

To save typing, you can shorten those commands by giving them aliases like this:

alias dv="dirs -v"
alias pd=pushd
share|improve this answer

You could use wildcards:

cd ../*test1 or even cd *t1
cd ../2010*test2
cd ../*bar

They are much more flexibles and you don't need to plan a script for each different situation, once you are used to them, you will use them for all kind of directories. Examples:

cd /e*/n*k will take you to /etc/network
cd /h* to /home

They are usable anywhere (ls also of course so you can filter what to see)

bash wildcards

share|improve this answer
This may or may not be useful, depending on how many file patterns the glob matches. if you have /hard_disk then cd /h* is less useful. – Rich Homolka Aug 4 '10 at 19:52
sure but if you use these commands a lot, you quickly find what is the match and it is almost always shorter than the full typing and you can use the others wildcards in the link. In your example, would be cd /ho* and yes... I agree this one isn't the best! :) but this is much more an exception and not the case of the OP as his directories names are long – laurent Aug 4 '10 at 20:15
  1. Try using the hist command to bring up the history of all commands you have executed.
  2. Create a shell script with the cd commands for each directories that you often cd to.
share|improve this answer
  • Tab completion
  • History access (especially in those shells that provide arrow key access)
  • In bash, consider using popd and pushd
share|improve this answer
Thanks! What shells provide arrow key access? – dehmann Aug 4 '10 at 16:19
Bash, tcsh. Maybe zsh. Probably some other. Start by just trying it. If that doesn't work, check the documentation. Search for "history" and possibly for "readline". – dmckee Aug 4 '10 at 16:23
Oh, you mean accessing previous commands by arrow key. Okay, I'm using that in bash. I thought you meant accessing one of the tab completion candidates using the arrow keys. – dehmann Aug 4 '10 at 16:24
New enough bashes also supports other readline features such as C-r for incremental history searching. As every shell is different the documentation is your friend. – dmckee Aug 4 '10 at 16:48
I've been using bash for years and only just discovered C-r in the past few months. It's a wonderful thing. – Doug Harris Aug 4 '10 at 17:42

In a question like this, i'd say to mention which shell you use. Each shell has slightly different tools. My answers here apply to bash, which is what you're probably using.

Also, to 'Frank Computer', you can't use a shell script, it creates it's own process, cd's in that process, exits, and your current shell is still where you are.

I think 'alias' is your friend. add something like:

alias CD_test2-'cd /absolute/path/to/foo/20100801-rev124-test2'

to your .bashrc (or .kshrc, or zshrc)

bash also has the env var CDPATH, which can be used as an anchor for cd'ing into relative paths. For example:

CDPATH=/usr:.; cd bin

would see that there is a /usr/bin and put you there, then if not, put you in ./bin. This probably won't help you, because you'd probably have to type out the entire subdir for this to work :

CDPATH=/root/to/all/testing/dirs:.; cd 20100801-rev124-test2

Methinks alias is the best you can get, at least in bash. There may be better tools in zsh.

share|improve this answer
I don't think scripts are the best way here but you can use a script to change directory if you source it calling it with . my_cd_script and put the cd command in the script. – laurent Aug 16 '10 at 4:16

Midnight Commander (mc) is a terminal-based (character mode) file manager. It has a hot-list feature that allows you to add directories to a list of those that you frequently use and select them from the list to cd to. It also has a quick cd feature that allows you to type in a full directory to cd into even while you're typing a command at the mc command line. You can also do the usual file manager navigation.

Another keystroke saver is to use the shell's CDPATH variable (it's available in most shells).

cd foo

If "foo" exists in the current directory, then cd will move you there. If not, it will search the directories in CDPATH and move you to the first "foo" it finds there.

share|improve this answer

quickcd is a nice script to change directory quickly by saving a lot of typing.

share|improve this answer

Take a look at fastcd Seems to be more useful than aliases

share|improve this answer

anc was designed exactly for that use case.

Here's an excerpt from the

Examples: # make the current directory the default anchor: $ anc s

# go to /etc, then /, then /usr/local and then back to the default anchor:
$ cd /etc; cd ..; cd usr/local; anc

# go back to /usr/local :
$ anc b

# add another anchor:
$ anc a $HOME/test

# view the list of anchors (the default one has the asterisk):
$ anc l
(0) /path/to/first/anchor *
(1) /home/usr/test

# jump to the anchor we just added:
# by using its anchor number
$ anc 1
# or by jumping to the last anchor in the list
$ anc -1

# add multiple anchors:
$ anc a $HOME/projects/first $HOME/projects/second $HOME/documents/first

# use text matching to jump to $HOME/projects/first
$ anc pro fir

# use text matching to jump to $HOME/documents/first
$ anc doc fir

# add anchor and jump to it using an absolute path
$ anc /etc
# is the same as
$ anc a /etc; anc -1

# add anchor and jump to it using a relative path
$ anc ./X11 #note that "./" is required for relative paths
# is the same as
$ anc a X11; anc -1

# using wildcards you can add many anchors at once
$ anc a $HOME/projects/*

# use shell completion to see a list of matching anchors
# and select the one you want to jump to directly
$ anc pro[TAB]

Full disclosure: I'm the author of anc

share|improve this answer

I've found that cd - greatly reduces typing, especially when a lot of the time is spent navigating back and forth between 2 directories. cd - changes to the previous directory.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .