3

We can open a parent folder of any file using nautilus script like

#!/bin/bash 

nautilus "$1"

If I do this script on symbolic link which is on the ~/Desktop, then it will open the Desktop itself.

How can I recode this script to open the parent of actual file symbolic link is bound to? For example, if I have a sym link file Desktop/my-doc.doc linking to the doc file located in /media/myharddisk/my-doc.doc, the script will open /media/myharddisk/ and NOT ~/Desktop in Nautilus.

ubuntico
  • 2,802
  • This is possible since Gnome Dash has such feature. You find a file and there is a button saying "Show in folder" – ubuntico Jan 27 '14 at 08:50

3 Answers3

2

Try this:

#!/bin/bash

file="$(readlink -f "$1")"
nautilus "${file%/*}"
kiri
  • 28,246
  • 16
  • 81
  • 118
2

Save this script as Open symlink's parent dir in your ~/.gnome2/nautilus-scripts/. On right click you will have an option under "scripts", "Open symlink's parent dir".

you can open the parent directory of any symbolic link from it. symbolic link may be of any file or folder. If it is not a symbolic link you will get a notice.

#!/bin/bash

if [ -h $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS ]; then
    var="$(readlink $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)"
else
    zenity --info --text="$(basename "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS") is not a symbolic link"
    exit 0
fi
if [ -d "$var" ]; then
    var2="$var"
else
    var2="$(dirname "$var")"
fi
nautilus "$var2"

enter image description here

enter image description here

If there is more than one soft link

You can use readmultilink from this answer by Radu Rădeanu . Then the script will be able to reach to parent directory navigating through multiple soft link at once.

You need to copy paste the function in your code and use readmultilink instead of readlink.

sourav c.
  • 44,715
  • Thanks. Your script is much smarter but I the simpler one works as well. however thanks for posting your as it's good to have it. – ubuntico Jan 27 '14 at 19:55
  • @souravc Apparently it doesn't work anymore. I get this zentity message "not a symbolic link". Although the script does work when I execute it from CLI – Porcupine Jan 17 '19 at 11:04
1

There is an an extra line feed at the end of "NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" that causes trouble when i try to add double quotes for the correct file detection.

I've changed the nice suggestion from souravc so it works perfect for me (line feed at the end removed, double quotes added so there no problems with filenames with spaces).

#!/bin/bash

# Remove line feed at the end of the path
selectedPath="${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS%?}"

# Check if the selected file is a symbolic link
if [ -h "$selectedPath" ]; then
    var="$( readlink "$selectedPath" )"
else
    zenity --info --text="$( basename "$selectedPath" ) is not a symbolic link"
    exit 0
fi

# Open the parent directory of the symbolic link 
nautilus "$( dirname "$var" )"
TuKsn
  • 4,370
  • 2
  • 26
  • 43
  • If you know there is an extra space, why use ${N_S_S_F_P%?} instead of ${N_S_S_F_P% }? – Adaephon Apr 08 '14 at 10:41
  • Another check with ascii signs shows a line feed at the end and not a extra space. I've changed the orginal post. I use ${N_S_S_F_P%?} because ${N_S_S_F_P%\n} didn't work. – TuKsn Apr 08 '14 at 13:39