Shell – open a file using CDPATH and symlink

directoryfilespathshellsymlink

To quickly move around, I added a path to CDPATH that contains symlinks to different locations. I did this by adding the following line to .bashrc:

export CDPATH=~/symlinks

When working with directories, everything's fine and I can access the symlinked folders from everywhere.

For example if I do:

$ ln -s ~/path/to/folder ~/symlinks/folder

I can then just write:

$ cd folder

to get inside the symlinked folder, regardless of my current directory.

However, when I create a symlink to a file and then try to open it with an editor, I get an empty file, unless I'm in the symlink directory.

For example if I do:

$ ln -s ~/path/to/file/filename ~/symlinks/filename

and then write:

$ kwrite filename

I get an empty file, if I'm not in the symlink folder.

I want to access the file from anywhere though, how can I achieve this?

Best Answer

The simple answer is that you can't.

What CDPATH does is that if you type "cd folder", it first checks if "folder" exists within your CDPATH; if not, it will check in the folder you're currently in. But this is specific for directory changes; kwrite doesn't check the CDPATH and AFAIK there's no configuration option to make it look in any specific directory.

What you could do is to make a small shell script that replaces kwrite, like this:

#!/bin/sh

FILE=$1

if [ -f "$HOME/symlinks/$FILE" ] 
then
   kwrite "HOME/symlinks/$FILE"
else
   kwrite "$FILE"
fi

Then run the script (which you could name e.g. "akwrite") instead of running kwrite directly.

Related Question