Ubuntu - Create a link to a specified directory so I can easily change to that directory

2

2

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.

user31289

Posted 2010-03-16T02:47:57.460

Reputation:

Answers

5

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

dmckee --- ex-moderator kitten

Posted 2010-03-16T02:47:57.460

Reputation: 7 311

+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 – 2010-03-16T06:50:54.457

4

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.

jlliagre

Posted 2010-03-16T02:47:57.460

Reputation: 12 469

+1 for the KISS approach instead of futzing with symlinks. For reference, see http://www.gnu.org/software/bash/manual/bashref.html#Shell-Builtin-Commands

– janmoesen – 2010-03-16T09:38:45.360

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 – 2010-03-16T14:00:37.097

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 – 2010-03-16T14:34:01.143

3

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:

#!/bin/sh
sed 's/file:\/\/\(.*\)/\1/' $HOME/.gtk-bookmarks | while read dir name
do
 ln -s $dir $HOME/bm/$name
done

mrucci

Posted 2010-03-16T02:47:57.460

Reputation: 8 398

Pretty cool stuff, though I'd redo that script in Ruby. Man are "real" shell scripts ugly. – Dan Rosenstark – 2010-05-17T09:41:43.463

I rewrote the proposed shell script... maybe now is more readable :) – mrucci – 2010-05-17T20:17:25.333

0

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

Justin Smith

Posted 2010-03-16T02:47:57.460

Reputation: 3 746

Why ln -s as opposed to just ln (hard link)? – Dan Rosenstark – 2010-05-17T09:37:03.317

@Yar, you can't hard-link directories – Brian – 2010-05-17T21:00:20.277