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

libreofficesymlink

Suppose I have a file bar.txt inside directory foo and create a symlink baz.txt to foo/bar.txt. Like:

./foo
./foo/bar.txt
./baz.txt -> foo/bar.txt

If I open baz.txt my editor will think baz.txt is opened in directory .. Is there a way to create a link such that rather bar.txt is (literally) opened?

Context (or why I'm trying to do this): I have a directory with a large collection of files which I index and comment inside an .odt file which remains in the same directory. In this .odt file I create hyperlinks to the indexed files in the directory, so that I can easily access the individual files with (much) more context than just the filename. I set LibreOffice to save the hyperlinks as relative paths, so that these links will work in all of my computers, which not always have the same directory tree to my user files.

I'd like to create a symlink (or equivalent) to this .odt file, but (in the terms of the above example) if the link opens baz.txt then relative paths (from the point of view of LibreOffice) will be wrong. The formerly created hyperlinks will not work, and if I happen to create an hyperlink in baz.txt (figuratively, of course) it won't work in the original bar.txt.

Best Answer

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.

Related Question