Is it possible to create a symlink to a directory, like /var/lib/tomcat6/webapps/MyWar that I can access from everywhere? I want to be able to say cd myapp from anywhere in the directory tree and go to that directory. Is it only possible in the directory where I create the symlink?

Do I have to update my ~/.bashrc file to include an alias like: alias myapp="cd /var/lib/tomcat6/webapps/MyWar" and then just type myapp from anywhere? What is the best way to handle this situation so I don't always have to type in the long directory? I also want to be able to use that parameter in say a copy command, so the alias wouldn't help in that situation. Hopefully I can do something similar where ~ maps to the home directory in any command.


If you put a link in your home directory you can do

$ cd ~/myapp

It means typing two extra characters, but it'll work.

Or you could use environment variables and do

$ cd $MYAPP
| improve this answer | |
  • +1 Setting the environment variable is the standard way to so this. Adding export MYWAR="/var/lib/tomcat6/webapps/MyWar" to your .bashrc file will allow you to do cd $MYWAR from anywhere. – Richard Holloway Mar 16 '10 at 6:50

Just set the CDPATH environment variable to .:/var/lib/tomcat6/webapps in your .[bash_]profile and you are set. Wherever you are, "cd myWar" will work as you expect it to do.

| improve this answer | |
  • +1 for the KISS approach instead of futzing with symlinks. For reference, see gnu.org/software/bash/manual/… – janmoesen Mar 16 '10 at 9:38
  • You know, I've been using various unix system for 20 years and didn't know this. Happen to know what shell introduced it? – dmckee --- ex-moderator kitten Mar 16 '10 at 14:00
  • I have been using it since more than 20 years but it is true CDPATH isn't at all a well know shell feature. As far as I know, it appeared in System V release 1 bourne shell (1983). – jlliagre Mar 16 '10 at 14:34

I keep a list of links in a folder in my home directory so that I can easily do:

cd ~/bm/myapp

Those "bookmarks" are generated and synchronized with the .gtk-bookmarks file (used by Nautilus and other GUI file managers) through the following shell script:

sed 's/file:\/\/\(.*\)/\1/' $HOME/.gtk-bookmarks | while read dir name
 ln -s $dir $HOME/bm/$name
| improve this answer | |
  • Pretty cool stuff, though I'd redo that script in Ruby. Man are "real" shell scripts ugly. – Dan Rosenstark May 17 '10 at 9:41
  • I rewrote the proposed shell script... maybe now is more readable :) – mrucci May 17 '10 at 20:17

ln -s /var/lib/tomcat6/webapps/MyWar ~/myapp and then you can do cd ~/myapp etc. the simlink can be used for most purposes exactly as if it were the actual directory (ie. cd will work, cp or mv will put a file in the directory rather than replacing the symlink with the file etc.).

| improve this answer | |
  • Why ln -s as opposed to just ln (hard link)? – Dan Rosenstark May 17 '10 at 9:37
  • @Yar, you can't hard-link directories – Brian May 17 '10 at 21:00

Your Answer

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