5

Using this answer, I have created a symbolic link in my .bashrc file to make changing to a frequently used directory easier.

E.g.

ln -s ~/a/b/c/d/development dev

I can change directory from my home dir to the development dir by entering cd dev. I can also enter ls dev from my home dir and that works too.

However, these commands only work in my home dir. If I enter them from anywhere else I get an error telling me No such file or directory.

If I enter cd ~/dev or ls ~/dev it works.

Can someone explain why that is and how I can fix it so I don't have to include ~/ in the path when I'm not in my home dir.

  • 3
    you don't want to put ln into a start up script, as the result is persistent. – ctrl-alt-delor Jan 19 '17 at 10:02
  • 1
    symlinks work that way: they are a like from a particular place, to another place. see this question for the answers you seek unix.stackexchange.com/questions/31161/… – ctrl-alt-delor Jan 19 '17 at 10:05
  • 3
    Yes, this just runs the same command every time you start a shell and that just results in an error message. You just need to run that once and the link is created for ever. – terdon Jan 19 '17 at 10:07
  • Ah, I didn't realise the links persisted. – ksl Jan 19 '17 at 13:39
11

Since you’re using Bash as your shell, you can use the CDPATH shell variable. The Bash manual describes it as

a search path: each directory name in CDPATH is searched for directory, with alternative directory names in CDPATH separated by a colon (‘:’)

You could add the following line to your .bashrc:

CDPATH=".:$HOME"

If you later type cd dev, the current working directory would be searched for a sub-directory named dev:

  • If such a directory exists, it changes into that directory (as the cd builtin command usually works).
  • If not, it would then search your home directory (~), find the symbolic link (realise that it’s a link to a directory) and change to the target directory (pointed to by ~/dev).

If you wanted to give preference to the directories within your home directory, you could list $HOME first in your CDPATH ("$HOME:.") but I would strongly advise against that as it breaks the principle of least surprise: the resulting behaviour differs too greatly from the standard.

9

You can't. How could you? The ln command creates a file that is a link to another file (or directory, or whatever). You can then use the link as though it were the original. But this is still just a file. It has a specific path, it's not just magically floating around and accessible from anywhere in the filesystem. Just like with any other file, you can't simply call it by name, you need to use its path.

Just like if you create a regular file:

$ cd ~/foo
$ touch file
$ cd ~/
$ ls file
ls: cannot access 'file': No such file or directory
$ ls ~/foo/file
file

A link is no different. You can't just cd cd dev from a random directory and expect it to work, just like you can't do cat file in a random directory and expect the system to magically know what file you want.

You can get the behavior you're looking for by using a script or a function, but not a link. For example, add these lines to your shell's initialization file (~/.bashrc for bash):

dev(){
    cd "$HOME"/dev
}

Or just:

alias dev='cd ~/dev'

Now, running dev will take you to ~/dev. Alternatively, define a variable in your ~/bashrc or ~/.profile:

dev="$HOME/dev"

You can now run cd $dev and be taken to ~/dev. This is possible in many ways, but links are not relevant.

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.