Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
@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 Answers 2

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.

share|improve this answer
    
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 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). –  Gilles Aug 21 '12 at 9:41
    
Thanks Gilles - sorry for missing that in the original! –  Jenny D Aug 21 '12 at 11:37
    
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

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
share|improve this answer
    
Thanks, I use Jenny D's solution for now, but I'll keep that in mind. –  bug Aug 21 '12 at 9:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.