8

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.

N0rbert
  • 99,918
  • 1
    Delete the broken link and recreate it. That is how symbolic links work. They refer to a file name. If that name is missing or changes, the link breaks. You cannot directly change the target of an existing link. – vanadium Aug 13 '18 at 16:46
  • @vanadium I understand how symbolic links work. But Midnight commander allows to change path of symbolic link's target. That is why I'm asking for GUI way. – N0rbert Aug 13 '18 at 17:03
  • 1
    MC will do the same behind the screens: delete and recreate the symlink. I do not know of a graphical file manager providing similar functionality. Perhaps the more power user oriented graphical dual pane file managers out there? – vanadium Aug 14 '18 at 09:22

2 Answers2

10

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.

muru
  • 197,895
  • 55
  • 485
  • 740
Ravexina
  • 55,668
  • 25
  • 164
  • 183
4

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.

  • Thank you! I added Caja related part as it trivial. I hope we have created very useful script. – N0rbert Aug 19 '18 at 09:05
  • 1
    @N0rbert Your edit reveals how similar writing a script in Caja File Manger is to Nautilus File Manager. Well done :) I did a second edit for language highlighting (prettify). Your use of cat > ... << \\EOF instead of gedit is great for distro neutrality. – WinEunuuchs2Unix Aug 19 '18 at 14:01