3

Having some problems googling this one because I'm not sure what the search term is. Shell scripting or terminal feature. What is the best way to streamline access to frequently used directories. Normally when I start a terminal it defaults to my home directory but sometimes I ike to open several terminal windows, the problem is I'm typing the cd /.../directory in each one multiple times and I need a way to not have to do this. What is the best way or just provide the search terms I could use to read about it online. Thank you

migrated from stackoverflow.com Nov 11 '11 at 3:08

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

  • create alias for each directory that you very often use. Also use cd - to go back to last directory that you accessed – Prince John Wesley Nov 10 '11 at 17:04
7

Create a symbolic link in your home directory:

$ ln -s path/to/a/really/deeply/nested/director/my-project ~/my-project

$ cd ~/my-project
4

Add a variable in your .bashrc:

MYPROJECT=path/to/a/really/deeply/nested/director/my-project

to use:

$ cd $MYPROJECT
4

Set the CDPATH variable: it contains additional directories to be searched when you run cd.

4

You can also take advantage of CDPATH variable.

And you could define a bash function in your $HOME/.bashrc like

 # in file ~/.bashrc
 function work() {
    cd $HOME/path/to/a/really/deeply/nested/director/my-project
 }

And recent bash or even better zsh may permit you things like

 cd **/my-project

Assuming you have only one deeply nested my-project/ directory in all your tree hierarchy. The ** is doing the equivalent of a find so can be slow.

2

You can also try autojump.

It is like cd command but with the ability to learn. It maintains a database of directories you visit and assign weight to each entry.

Later you can use j foo to jump to a directory that contains foo in its name. If more than few directories have foo, it will show you a list of matches from where you can select your desired directory.

  • Autojump works great. I hope people read this answer as well :) – Damian Nadales Aug 25 '18 at 20:20
1

You could use a bookmarks for the shell, e.g. bookmarks.sh.

1

The author of CDPATH recommends to export CDPATH to the environment. Please keep in mind that there is a strong chance that this will subtly break shell scripts that expect directories to be in one specific place. A remedy is to put CDPATH into ~/.bashrc and not export it. Your ~/.bashrc would look like this:

CDPATH=/var/:/etc/

Note that sourced shell scripts will still pick up CDPATH in this case. autojump is certainly a much safer alternative and more convenient to use than CDPATH.

0

Add an alias in your .bashrc

alias myproject="cd path/to/a/really/deeply/nested/director/my-project"

to use:

$ myproject
0

anc is designed exactly for this use case.

https://github.com/tobimensch/anc

Here's an excerpt from the README.md:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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