Ubuntu: Open the parent folder of a symbolic link via right-click?


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.


Try this:

#!/bin/bash    file="$(readlink -f "$1")"  nautilus "${file%/*}"  


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.


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" )"  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »