3

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?

1
  • @Jenny P: Thanks, now accessing files with kwrite works great, but I guess it would be quite tedious to make my CDPATH and symlink combination work with functions like mv or cp? I guess Gilles' solution with the shell variables is more "flexible"?
    – bug
    Commented Aug 21, 2012 at 12:35

2 Answers 2

5

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.

7
  • Thanks, that works quite well, except for file names that contain spaces, where kwrite opens two new files (the names being the parts before and after the space).
    – bug
    Commented Aug 21, 2012 at 9:15
  • 1
    @user22310 The script was missing double quotes around variable substitutions, causing the filenames to be split. Always put double quotes around variable substitutions (see $VAR vs ${VAR} and to quote or not to quote). Commented Aug 21, 2012 at 9:41
  • I'm beginning to wonder if you're not complicating things a bit too much with your symlink directory. What is the problem you're trying to solve by having a directory full of lots of symlinks?
    – Jenny D
    Commented Aug 21, 2012 at 12:34
  • I want to quickly access specific folders and files on the shell.
    – bug
    Commented Aug 21, 2012 at 13:20
  • The main problem I see with this solution is that you risk mixing things up by thinking you're working on a symlink, when in fact you're working on the original, or vice versa. You spoke about using "mv" - but if you move a symlink, the original will not be moved, you'll just rename the symlink. And you're getting used to not having to consider the paths of files, which will bite you in a tender place when you work on another system. Also, what do you do when you have more than one file with the same name but in different directories?
    – Jenny D
    Commented Aug 21, 2012 at 13:58
0

You can't.

What you can do is define shortcuts as shell variables.

fn=~/path/to/file/filename
kwrite $fn

In zsh, you can avoid name clashes with shell variables by using directory hashes instead. These are aliases for file names that are expanded after ~ (like user names). Despite their names, these aliases don't have to point to a directory, they can be expanded to arbitrary text.

hash -d fn=~/path/to/file/filename
kwrite ~fn
1
  • Thanks, I use Jenny D's solution for now, but I'll keep that in mind.
    – bug
    Commented Aug 21, 2012 at 9:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .