Ubuntu – How to edit target path of broken symbolic link from GUI

filemanagerguisymbolic-link

Let's assume the following situation:

  1. I have file1 somewhere

    $ touch file1
    
  2. I make symbolic link to file1 as symlink1:

    $ ln -s file1 symlink1
    $ file symlink1 
    symlink1: symbolic link to file1
    
  3. I rename file1 to new name (now symlink1 is broken)

    $ mv file1 file2
    $ file symlink1 
    symlink1: broken symbolic link to file1
    

After the last step the symlink1-link is broken.

I know that Midnight Commander has File → Edit symlink option, but it is terminal way:

Edit symlink in Midnight Commander

$ file symlink1 
symlink1: symbolic link to file2

and it is very useful if target and symlink are located in different file-systems and/or nested folders.

As far I can see Nautilus, Caja, Nemo, Thunar and Dolphin do not have this functionality.

Update. The most useful solution for me would be integration with Caja file-manager by Caja-actions. I use Caja on daily basis.

Best Answer

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.