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?

bug
  • 2,518
  • @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 Aug 21 '12 at 12:35

2 Answers2

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.

Jenny D
  • 13,172
  • 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 Aug 21 '12 at 09: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). – Gilles 'SO- stop being evil' Aug 21 '12 at 09: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 Aug 21 '12 at 12:34
  • I want to quickly access specific folders and files on the shell. – bug Aug 21 '12 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 Aug 21 '12 at 13:58
  • I'm working on a system with several different config directories. Instead of making a maze of twisty little symlinks, I've made aliases to quickly go to the directory where the files live. E.g.: 'alias cdws=cd /opt/apache/servers'. This gives me a faster way to move around the file system, while still keeping track of where I am and where the different files are. – Jenny D Aug 21 '12 at 14:01
  • These links are used for my personal files, I won't have to use them on another system. Also, if I had to deal with different files that have the same name, I guess I'd just give my symlinks different names. But the other problems stand, and that's why I dropped the symlinks + CDPATH solution in favor of a shell variables and aliases combination for now. – bug Aug 22 '12 at 09:08
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