Linux Gnome: Add Open Terminal Here / Open Shell Prompt Here Right Click Menu To a File Manager

Posted on in Categories , , , , , , , , last updated November 5, 2008

Question: How do I open a shell prompt or gnome-terminal at the current location while browsing directories and files via nautilus file manager? I’d like to see Open Terminal / Prompt here option added to my right click menu. How do I open command prompt in a single selected directory or otherwise in current directory?

Answer: Nautilus can execute script that can open a gnome-terminal at the current location or selected directory location. All such scripts will be available via right click menu option.

Nautilus Scripts

Nautilus is the official file manager for the GNOME desktop. It has ability to run add-on scripts written in any scripting language (or binary file) without a problem. All add-on scripts must be created and stored at ~/.gnome2/nautilus-scripts ($HOME/.gnome2/nautilus-scripts) location.

Create Open Terminal Here (Open Shell Prompt Here) Shell script

Create file as follows using gedit text editor:
$ gedit "$HOME/.gnome2/nautilus-scripts/Open Terminal Here"
Append shell script code:

#!/bin/bash
# From Chris Picton
# Replaces a Script by Martin Enlund
# Modified to work with spaces in path by Christophe Combelles
 
# This script either opens in the current directory, 
# or in the selected directory
 
base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
     dir="$base"
else 
     while [ ! -z "$1" -a ! -d "$base/$1" ]; do shift; done
     dir="$base/$1"
fi
 
gnome-terminal --working-directory="$dir"

NAUTILUS_SCRIPT_CURRENT_URI variable gives current location for directory. NAUTILUS_SCRIPT_SELECTED_FILE_PATHS sets a newline-delimited paths for selected files.

Save and close the file. Now, setup permissions, enter::
$ chmod +x "$HOME/.gnome2/nautilus-scripts/Open Terminal Here"
And you are done. Open nautilus file manager, select directory > Right Click > Scripts > Open Terminal Here:

Fig.01: Open Shell Prompt Here Script
Fig.01: Open Shell Prompt Here Script

And terminal will open at sg1 directory:
Fig.02: Nautilus script opened a terminal
Fig.02: Nautilus script opened a terminal

Further readings:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

36 comment

      1. No, installing the correct script is the better option and if it doesnt work then raise it a a bug until it does work. Adding “yet another script to replace something broken” i have the problem with the Linux/Gnu infrastucture – duplication and papering over cracks instead of fixing the foundations.

  1. The Scripts option will only appear in the Nautilus File menu and Right click menu if there are scripts already present in the $HOME/.gnome2/nautilus-scripts/ directory. If this is your first script, in that directory, you will need to logout of X Windows and then login again to get Nautilus to recognize that scripts are present and add the Scripts option to its menus.

  2. Very nice.
    Though I wish I could add a button to the menu bar to just open the terminal.
    There’s an app like that for the mac, and it just saves you the time to select the folder first and go to a sub-menu.

  3. Awesome, thank you! For some reason, one directory I have here is named “cd ..”. I have no idea how that came about. Of course, with the Shell you can’t navigate to it. But from Nautilus I could and then I opened the Shell from there – thanks to your script. Very helpful!

  4. Worked like a charm, THANKS!
    For the really non-coder user, there is a point-n-click way to make the script:
    1. Go to the Home folder, click View>Show Hidden Files from the menu.
    2. Go into the .gnome2/nautilus-scripts/ folder
    3. Right-click > Create Document > Empty File
    4. Rename the file to “Open Terminal Here”
    5. Open the file in simple text editor.
    6. Copy-paste all the code given above in here. Save and close and go back to the folder.
    7. On the file again, Right-click > properties , see the “Permissions” tab, check ON the “Allow executing file as program” option, and click on Close.
    –Now it should work – on any folder, right-click and you’ll see the Scripts > Open Terminal Here option.

  5. @Nikhil

    The line –> $ gedit “$HOME/.gnome2/nautilus-scripts/Open Terminal Here” ) with none of the normal text to the left of the cursor (user name and path). (I think the carat means Terminal wants me to type more, but I have no idea how to make it go away.)

    …and the only way I have figured out to get out of that situation is to close Terminal and open it again.

    …which means I kept having to cd, cd, cd, and cd some more.

    …which is especially annoying because my directories have spaces in them, and I have learned that Linux can’t handle those without adding a preceding character (sorry, but that’s extremely lame), and that character is the backslash, and my [\] key is right beside my [Enter] key, making the process error-prone and throwing up another stumbling block.

    …so I thought there must be a way to end *that* madness.

    ……and luckily there was, and it was the instructions on this page, and for the first time today, something worked!

    Again, thank you. :)

  6. My comment got mangled, and does not make sense the way it was posted. What a strange error.
    Here it is is smaller pieces, so maybe it will post correctly…

    @Nikhil

    The line –> $ gedit “$HOME/.gnome2/nautilus-scripts/Open Terminal Here” <– did *not* work for me (and I wouldn't have known how to 'append' from the command line anyway).

    The instructions you gave *did* work, and made it clearer what I was doing (creating a file).

    THANK YOU for your very clear explanation and your somehow superior method!!

    Extended story (begin absolute Linux newb venting):

    Today I tried to use the terminal for the **first time ever**(!!) because I was renaming a bunch of files and then thought "Hey… This is Linux, and I think you can do fancy things on the command line in Linux. Maybe there is a better way to do this renaming-many-files task."

  7. I have spent the last hour or two trying and trying to figure out how to use the ‘rename’ command to replace a certain string in multiple files’ filenames with a different string, but **nothing works**, *and* every 3rd thing I try makes the command line…I mean Terminal…seem to freeze or just change the prompt to a lonely, confusing carat sign (>) with none of the normal text to the left of the cursor (user name and path). (I think the carat means Terminal wants me to type more, but I have no idea how to make it go away.)

    …and the only way I have figured out to get out of that situation is to close Terminal and open it again.

    …which means I kept having to cd, cd, cd, and cd some more.

  8. …which is especially annoying because my directories have spaces in them, and I have learned that Linux can’t handle those without adding a preceding character (sorry, but that’s extremely lame), and that character is the backslash, and my [\] key is right beside my [Enter] key, making the process error-prone and throwing up another stumbling block.

    …so I thought there must be a way to end *that* madness.

    ……and luckily there was, and it was the instructions on this page, and for the first time today, something worked!

    Again, thank you. :)

Leave a Comment