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
10 Answers
Create a symbolic link in your home directory:
$ ln -s path/to/a/really/deeply/nested/director/my-project ~/my-project
$ cd ~/my-project
-
Nice solution. Then it's good to know the difference between
cd -L ~/my-project
andcd -P ~/my-project
. Aug 20, 2019 at 9:49 -
Will this need to be saved in a
.bashrc
file, or will this symbolic link persist? Mar 2, 2021 at 22:40 -
1@MadmanLee - No, once a soft link is created with
ln -s
, it stays in the filesystem permanently (until deletion). On the contrary (and perhaps this motivates your question),alias
es are not permanent, and should be recreated each time, e.g. in.bashrc
. Jul 15, 2021 at 10:39
Add a variable in your .bashrc:
MYPROJECT=path/to/a/really/deeply/nested/director/my-project
to use:
$ cd $MYPROJECT
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.
Set the CDPATH variable: it contains additional directories to be searched when you run cd
.
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 :) Aug 25, 2018 at 20:20
Add an alias in your .bashrc
alias myproject="cd path/to/a/really/deeply/nested/director/my-project"
to use:
$ myproject
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.
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.
I've written a script for that. Here's the link to it: https://github.com/aderchox/pev
All instructions are on the GitHub page already, but a brief description of it is also as follows.
Add a directory to your list like this:
pev add myDir /path/to/my/dir
Now typing:
pev show
will show:
| NAMES VALUES
| ---------- ----------
| pev_myDir "/path/to/my/dir"
Now to cd to the myDir directory, you can easily type this from anywhere:
cd $pev_myDir
You will also be able to do any other thing like this too:
# copy "file" from another path to the "myDir" directory
cp /path/to/file $pev_myDir
Hope that helps.
-
-1. Feedback: Not quoting variables is usually OK in zsh. The question is tagged
bash
, your code uses Bash-specific syntax, the documentation mentions.bashrc
. In Bash you should double-quote or else bad things will happen. This is enough for me to consider your code (and answer) flawed. The downvote is not to punish you for your willingness to help; it's only to warn other users. If you debug the code then I will revoke my vote. (Leave me a comment starting with @Kamil to get my attention). Aug 20, 2019 at 9:28
alias
for each directory that you very often use. Also usecd -
to go back to last directory that you accessed