1

My goal is to be able to right-click a symbolic link in nemo, triggering a custom script that forces the link to be re-written as a relative path, should it happen to possess an absolute path upon the act.

The problem is that DRAG+SHIFT+CTRL is hard-coded within nemo to to produce an absolutely-pathed symlink. This causes problems when I attempt to access the file system remotely.

My problem is I would like to retain the DRAG+SHIFT+CTRL gesture, I would not like to build nemo from source, and I would like to be able to browse any number of irrelevantly-linked directories remotely.

My proposed solution is to create a new context-menu option within nemo that will allow me to "fix" any one of these links by triggering a script.

I've successfully created the context-menu option by creating a new .nemo_action file using the template at GitHub.

There are two directories for these types of files:

  • /usr/share/nemo/actions/ for system-wide actions
  • $HOME/.local/share/nemo/actions/ for user actions

my fix-link.nemo_action file is as follows:

[Nemo Action]
Active=true
Name=Force Relative Link
Comment=If the file is a link to an absolute path, it will be forced to become relative.
Exec=<fix-link.sh %F>
Selection=any
Extensions=nodirs;
EscapeSpaces=true

and my fix-link.sh file (in the same directory) is as follows:

#!/bin/bash
$(ln "-rs" $(readlink -f "$1") "$1.tmp")
mv -f "$1.tmp" $(readlink -f "$1")
exit 0

My temporary file is being created just fine, but it refuses to overwrite the filepath returned by $(readlink -f "$1"). I think it might have something to do with permissions set by nemo when I'm initially DRAG+SHIFT+CTRLling to create the absolutely-pathed link.

EDIT

@Gilles fixed the many errors in my script, at which time I discovered that I also needed to change the Extensions=nodirs; line in the fix-link.nemo_action file to read Extensions=any;, otherwise I wouldn't ever be able to activate this script through the context menu on even a link to a directory, which was the whole purpose for this thing.

SO NOW

fix-link.nemo_action is as follows:

[Nemo Action]
Active=true
Name=Force Relative Link
Comment=If the file is a link to an absolute path, it will be forced to become relative.
Exec=<fix-link.sh %F>
Selection=any
Extensions=any;
EscapeSpaces=true

and my fix-link.sh file is as follows:

#!/bin/bash
ln -rs -- "$(readlink -f -- "$1")" "$1.tmp"
mv -f -- "$1.tmp" "$1"

and all is right in the world. Thanks everybody.

2
  • 1
    rn used to be a Usenet reader -- did you mean mv instead?
    – Jeff Schaller
    Aug 8 '15 at 2:56
  • @JeffSchaller yes.
    – user97025
    Aug 8 '15 at 3:04
0

There are several problems with your script.

  • Missing double quotes around $(readlink …). That will break if the link target contains whitespace or wildcard characters.
  • You're using a command substitution around the ln command, and using the result as a command to run. Fortunately this does no harm since ln produces no output.
  • Your mv command moves the symbolic link to the target location, which makes no sense.

I think this is what you meant to write:

#!/bin/sh
ln -rs -- "$(readlink -f -- "$1")" "$1.tmp"
mv -f -- "$1.tmp" "$1"

Alternatively, you could use the symlinks utility (originally by Mark Lords, now maintained by J. Brandt Buckley), present in many Linux distributions. The command symlinks -c /path/to/directory converts all symbolic links in the specified directory to relative links.

2
  • You are correct! That is exactly the script I was trying to write.
    – user97025
    Aug 8 '15 at 16:41
  • sadly, even though my question has been answered, the slightly greater goal has not been met. see this question
    – user97025
    Aug 9 '15 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy