2

Is it possible to show the target of a symbolic link in Nautilus as a column in the listing of a folder? Please don't point me to ls -l, I do know about it and use it, but it would be nice for UI users to have a UI way to see the link target in the listing.

pa4080
  • 29,831
soriak
  • 303
  • 3
  • 11

2 Answers2

5

This goal can be achieved via Nautilus Script.

Source of this answer is the answer's section of the question: If I create a link to a folder, how can I get from that linked folder to the "real" folder from within Nautilus?

Also here is the origin of the script: open-the-link-target-in-nautilus. Listed dependencies are perl and zenity, but they are already installed by default.

There is no difference into the script content but in nowadays versions of Ubuntu the location path for Nautilus Scripts is changed to: ~/.local/share/nautilus/scripts

1. Create the script file and make it executable:

touch ~/.local/share/nautilus/scripts/"Open link target"
chmod +x ~/.local/share/nautilus/scripts/"Open link target"

2. Edit the script file:

gedit ~/.local/share/nautilus/scripts/"Open link target" &

3. Paste next lines as the script's content:

#!/bin/bash
#Title=Open-the-link-target-in-nautilus
#Title[fr]=ouvrir-le-repertoire-cible-dans-nautilus

#==============================================================================
#                     open-the-link-target-in-nautilus
#
#  author  : SLK
#  version : v2011051501
#  license : Distributed under the terms of GNU GPL version 2 or later
#
#==============================================================================
#
#  description :
#    nautilus-script : 
#    opens the target of a symbolic link of the selected object; if 
#    the target of the symbolic link is a file, opens the parent folder
#
#  informations :
#    - a script for use (only) with Nautilus. 
#    - to use, copy to your ${HOME}/.gnome2/nautilus-scripts/ directory.
#
#  WARNINGS :
#    - this script must be executable.
#    - package "zenity" must be installed
#
#==============================================================================

#==============================================================================
#                                                                     CONSTANTS

# 0 or 1  - 1: doesn't open but displays a message
DRY_RUN=0

#------>                                       some labels used for zenity [en]
z_title='Open the link target in nautilus'
z_err_bin_not_found='not found\nEXIT'
z_no_object='no object selected\nEXIT'
z_info_target='path of the target'
z_choice_open_nautilus='open target in nautilus'
z_choice_open_file='open file with default application'
z_choice_display_filepath='open a messagebox to copy filepath'

#------>                                       some labels used for zenity [fr]
#z_title='ouvrir le repertoire cible dans nautilus'
#z_err_bin_not_found='introuvable\nEXIT'
#z_no_object='aucun objet selectionne\nEXIT'
#z_info_target='chemin de la cible'
#z_choice_open_nautilus='ouvrir la cible dans nautilus'
#z_choice_open_file='ouvrir le fichier avec le programme par defaut'
#z_choice_display_filepath='ouvrir une boite de dialogue affichant le chemin du fichier'
#==============================================================================
#                                                                INIT VARIABLES

# may depends of your system
DIRNAME='/usr/bin/dirname'
GREP='/bin/grep'
NAUTILUS='/usr/bin/nautilus'
PERL='/usr/bin/perl'
READLINK='/bin/readlink'
XDG_OPEN='/usr/bin/xdg-open'
ZENITY='/usr/bin/zenity'

#==============================================================================
#                                                                     FUNCTIONS

function check_bin
{
    err=0
    for bin in $* ; do
        if [ ! -x "$bin" ] ; then
            $ZENITY --error --title "$z_title" \
              --text="$bin $z_err_bin_not_found"
            err=1
        fi
    done
    [ $err -eq 1 ] && exit 1
}
#==============================================================================
#                                                                          MAIN

# lets check for required binaries :
[ -x "$ZENITY" ] || {
    echo "[ERROR] $ZENITY not found : EXIT"
    exit 1
}
check_bin "$DIRNAME" "$GREP" "$NAUTILUS" "$PERL" "$READLINK"

# lets check if object is selected :
[ "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" == "" ] && {
    $ZENITY --error --title "$z_title" \
      --text="$z_no_object"
    exit 1
}

# retrieve the first object selected :
first_object=`echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" \
  | $PERL -ne 'print;exit'`

# lets check if local path :
[ `echo "$first_object" | $GREP -c "^/"` -eq 0 ] && {
    $ZENITY --error --title "$z_title" \
    --text="[ERROR] $first_object has not a valid path\nEXIT"
    exit 1
}

# retrieve the target path :
if [ -L "$first_object" ] ; then
    # symbolic link
    target=`$READLINK -f "$first_object"`
else
    # not a symbolic link :
    target="$first_object"
fi

if [ -d "$target" ] ; then
    # target is a directory
    target_to_open_in_nautilus="$target"
else
    # target is a file, let's take the parent directory
    target_to_open_in_nautilus=`$DIRNAME "$target"`
fi

### DRY RUN : noop

[ $DRY_RUN -eq 1 ] && {
    $ZENITY --info --title "$z_title" \
      --text="<b>DRY RUN</b>
first_object: $first_object
target: $target
target_to_open_in_nautilus: $target_to_open_in_nautilus"
    exit 0
}

### GO : let's open

choice=`$ZENITY --list --title="$z_title" --width="500" --height="200" \
  --text="<b>$z_info_target</b>\n$target" \
  --radiolist --column "" --column "action" \
  TRUE "$z_choice_open_nautilus" \
  FALSE "$z_choice_open_file" \
  FALSE "$z_choice_display_filepath"`

case $choice in
    "$z_choice_open_nautilus")
        $NAUTILUS --no-desktop "$target_to_open_in_nautilus"
    ;;
    "$z_choice_open_file")
        $XDG_OPEN "$target"
    ;;
    "$z_choice_display_filepath")
        $ZENITY --entry --title="$z_title" --width="500" \
          --text="$z_info_target" \
          --entry-text="$target" &
    ;;
    *)
        exit 1
    ;;
esac

exit 0

### EOF

4. Save the file, open Nautilus, 'right click' on some symlink, go to 'Scripts section' and click on 'Open link target':

enter image description here

5. Then will be appeared this dialogue box:

enter image description here


I'm afraid that, if you want to add a new column into the list view, the source code of Nautilus must be changed and recompiled.

derHugo
  • 3,356
  • 5
  • 31
  • 51
pa4080
  • 29,831
  • 1
    Works on Ubuntu 22.04, GNOME nautilus 42.2. Used Kevin Kane's patch to highlight target file too. Since I open the target 90% of time and copy the filepath 10% of time, I'm considering splitting the full script with menu box into 2 or 3 different scripts so I can skip the menu box. Opening link in new tab instead of new window, or even in same tab, would be a nice option too, but I don't know about an option with the command line. Opening in same tab may be too low-level. – hsandt Mar 06 '23 at 19:09
  • Hello @hsandt. Thank you letting us (the community) know, this approach still work. Unfortunately, right now, I don't have a time to implement the features you've mentioned, but I believe you can do it on your self by the help of the anvwers given under this question: https://askubuntu.com/q/55656/566421 – pa4080 Mar 07 '23 at 11:05
1

The answer provided by pa4080 worked well for me on Ubuntu 16.04. However, for links to files I found it is more helpful if the new Nautilus window opens with the target file selected. This is similar to when doing a find within Nautilus, if one right clicks on one of the search results and chooses Open Item Location.

To achieve this, I replaced the following section

if [ -d "$target" ] ; then
    # target is a directory
    target_to_open_in_nautilus="$target"
else
    # target is a file, let's take the parent directory
    target_to_open_in_nautilus=`$DIRNAME "$target"`
fi

with

target_to_open_in_nautilus="$target"

Now the new Nautilus window will open with the link's target selected when it is a file.

Kevin Kane
  • 11
  • 3