How to create a symlink which opens (literally) the target file

No. But you can create a libreoffice wrapper that'll take each argument that is a symlink and turn it into $(readlink -f $the_symlink). You can then set your file manager to open libreoffice files through that wrapper.

lowrapper:

#!/bin/bash -e
args=()
for a; do
    case $a in 
        -*) args+=("$a");;  #skip flags (your file names don't start with -, right?)
        *)  if ! [ -L "$a" ]; then #not a link
                args+=("$a")
            else #link => target
                args+=( "$( readlink -f "$a")" )
            fi
            ;;
    esac
done
libreoffice "${args[@]}"

Now if you chmod +x lowrapper, put it in some directory of your PATH, and then change the handler program of your libreoffice files from libreoffice to lowrapper, then libreoffice will be opening the link targets instead of the links.


You can't, but what you can do instead is create symlinks to your "foo" directory like so:

./foo
./foo/root -> .
./root -> foo/.

Then append "root" to the start of all your hyperlinks. Now if you open your document from the ".", "root" will be resolved into "foo/.", and your "root/baz.txt" will resolve into "foo/./baz.txt". If opened from the "foo" itself, the same "root/baz.txt" will be resolved into "./baz.txt" because "root" points to ".".


I don't think there's any way to do exactly what you're asking for. Symlinks are file system constructs. However, there might be a decent workaround if you create a little script that opens the link target instead of the link:

#!/bin/bash

i=0
declare -a targets
for file in "$@"; do
        targets[$i]="$(readlink -f "$file")"
        ((i++))
done
libreoffice "${targets[@]}"

Save that in your PATH, for example as ~/bin/openLink.sh and make it executable:

chmod a+x ~/bin/openLink.sh`

Now, open your file manager (you mentioned caja), right click on an .odt file and choose "Open With" => "Other Application":

open with dialog

Click on the "Use a custom command" and put your script there:

custom command dialog

Finally, close everything. Now, each time you click on an .odt file, it will be opened using that wrapper so it should open all links in their target directory. Note that readlink -f on a regular file just returns the name of the file, so this will also work for non-links.