How to edit target path of broken symbolic link from GUI?

I use thunar as my default file manager, and here is what I suggest to get a similar behavior like mc.

Create a simple shell script, and save it somewhere you like, for the demonstration purpose I saved it at my $HOME:

#!/bin/bash
if [ -L "$1" ];
then
 new_address=$(zenity --entry)
 ln -sf "$new_address" "$1"
else
 zenity --error --text 'This is not a link'
fi

Add a new custom action which runs this script, for example in thunar:

enter image description here

Now I can right click on files and select 'relink', and it will asks for a new address for that link:

enter image description here

This is obvious that you have to install zenity package to use this script, I think you can manage to use it in different file managers.


Edit Symbolic Link in Nautilus

The script

To do this in Nautilus we need to create a script using:

mkdir -p ~/.local/share/nautilus/scripts
gedit ~/.local/share/nautilus/scripts/edit-link

Paste in the following:

#!/bin/bash

# NAME: edit-link
# PATH: $HOME/.local/share/nautilus/scripts
# DESC: Edit symbolic link
# CALL: Called from Nautilus file manager.
# DATE: August 18, 2018.

# strip new line char passed by Nautilus
FILENAME=$(echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | sed -e 's/\r//g')

# Multiple files can't be selected.
LINE_COUNT=$(wc -l <<< "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS")
LINE_COUNT=$((LINE_COUNT-1))

if [[ $LINE_COUNT > 1 ]] ; then
    zenity --error --text "Ony one file can be selected at a time! "
    exit 1
fi

# Object type must be "file..." (ie no directories, etc.)
if [ -d "${FILENAME}" ] ; then
    zenity --error --text "$FILENAME is a directory!";
    exit 1
else
    if [ -L "${FILENAME}" ]; then
        : # Bash noop
    else
        zenity --error --text "${FILENAME} is not a symbolic link!";
        exit 2
    fi
fi

NewLink=$(zenity --entry --text "Enter new symbolic link")
ln -sf "$NewLink" "${FILENAME}"

exit 0

and make it executable

chmod +x ~/.local/share/nautilus/scripts/edit-link

Sample output

This is the test data used. The second last section shows the broken link. Then our script is run giving a new file name. The last section shows the new good link.

Edit Link2

Sample screen

This is what the script looks like when you run it:

edit link 1.png

  • Highlight a broken link with Nautilus
  • Right click for context menu
  • Select Scripts
  • Select edit-link
  • Enter new file name above and click OK button

Edit Symbolic Link in Caja

The method is similar to Nautilus but with some Caja specifics. We should follow GNOME2→MATE Migration guide.

So we need create script in the ~/.config/caja/scripts:

mkdir -p ~/.config/caja/scripts
cat > ~/.config/caja/scripts/edit-link << \EOF
#!/bin/bash

# NAME: edit-link
# PATH: $HOME/.config/caja/scripts
# DESC: Edit symbolic link
# CALL: Called from Caja file manager.
# DATE: August 19, 2018.

# strip new line char passed by Caja
FILENAME=$(echo $CAJA_SCRIPT_SELECTED_FILE_PATHS | sed -e 's/\r//g')

# Multiple files can't be selected.
LINE_COUNT=$(wc -l <<< "$CAJA_SCRIPT_SELECTED_FILE_PATHS")
LINE_COUNT=$((LINE_COUNT-1))

if [[ $LINE_COUNT > 1 ]] ; then
    zenity --error --text "Ony one file can be selected at a time! "
    exit 1
fi

# Object type must be "file..." (ie no directories, etc.)
if [ -d "${FILENAME}" ] ; then
    zenity --error --text "$FILENAME is a directory!";
    exit 1
else
    if [ -L "${FILENAME}" ]; then
        : # Bash noop
    else
        zenity --error --text "${FILENAME} is not a symbolic link!";
        exit 2
    fi
fi

NewLink=$(zenity --entry --text "Enter new symbolic link")
ln -sf "$NewLink" "${FILENAME}"

exit 0
EOF

and make it executable

chmod +x ~/.config/caja/scripts/edit-link

Then we can use this script from Caja Scripts drop-down menu.