Bash – How to define a symbolic link that I can use in every directory

bashsymlink

Is it possible to set a symbolic link so that I can use project to point the directory home/me/project, e. g.? This should be independent of the location in the file system.

I'd like to use commands like cd project, nano project/file1.tex and so on.

Do I have to write a symbolic link to all of my directories?

Best Answer

Most shells have a CDPATH variable that cd can lookup for directories to change to in the same way that executables are searched in $PATH.

So if you add your symlinks in a ~/projects directory and do CDPATH=~/projects, you'll be able to do cd foo to go in ~/projects/foo

With zsh, if $var contains a path you can do cd ~var to cd to that path. The useful part of that is when your prompt has %~ which then reflects it in your prompt:

$ proj1=/usr/local proj2=/etc/apache2
$ PS1='%~$ '
$ cd ~proj1
~proj1$ cd ~proj2/sites-enabled
~proj2/sites-enabled$

With setopt cdablevars, you can also do cd proj1 instead of cd ~proj1.

Related Question