5

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 ../2010080Tab2Tab-rev12Tab6-Tabbar

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.

5
  • Hmm, unless the solution requires coming up with a bash script or similar.
    – dehmann
    Aug 4, 2010 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, 2010 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, 2010 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, 2010 at 16:20
  • alias rotd='pushd -0' then use rotd to rotate directory stack. Nov 12, 2016 at 16:38

11 Answers 11

3

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
2

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

2
  • 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. Aug 4, 2010 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, 2010 at 20:15
1
  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.
1
  • Tab completion
  • History access (especially in those shells that provide arrow key access)
  • In bash, consider using popd and pushd
5
  • Thanks! What shells provide arrow key access?
    – dehmann
    Aug 4, 2010 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". Aug 4, 2010 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, 2010 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. Aug 4, 2010 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. Aug 4, 2010 at 17:42
1

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.

1
  • 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, 2010 at 4:16
1

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.

0

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

0

anc was designed exactly for that use case.

https://github.com/tobimensch/anc

Here's an excerpt from the README.md:

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

0

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.

0

Take a look at https://pypi.org/project/fastcd/ Seems to be more useful than aliases

0

There are various shells and shell extensions that support partial path autocomplete and fish is one of them. For example cd /v/l/fsck Tab will convert the path to /var/log/fsck/. cd /u/s/appl Tab will expand the path to /usr/share/applications/

You can also use some fuzzy search software like

  • fuzzy bash completion

    This bash completion module performs "fuzzy" matching similar to textmate's "Go to File" fuzzy matching, "flex matching" in emacs' ido mode, etc.

      mkdir pizza
      mkdir jazz
      cd zz<TAB>
      # displays `pizza' and `jazz'
      rm -r jazz
      cd zz<TAB>
      # completes the word `pizza'
    
  • fzf: a general-purpose command-line fuzzy finder. Just press Alt-C to cd into the selected directory

    Demo: https://www.youtube.com/watch?v=qgG5Jhi_Els

  • Command-T:

    Command-T is a Vim plug-in that provides an extremely fast "fuzzy" mechanism for:

    Command-T in action

See also

You must log in to answer this question.

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