Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
ls 
cd ../20100801-rev124-test1
ls 
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.

UPDATE:

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 stackoverflow.com 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
1  
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
add comment

7 Answers

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
add comment

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

share|improve this answer
add comment

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
add comment

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).

CDPATH=/some/top/dir:/another/one
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
add comment

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
add comment
  • 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
1  
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
1  
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
1  
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
add comment
  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
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.