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

by on November 5, 2008 · 36 comments· LAST UPDATED November 5, 2008

in , ,

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:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 36 comments… read them below or add one }

1 Matias Lopez Riglos November 5, 2008 at 6:22 pm

It’s an additional “#” at the end of the first line. You must remove it or the script doesn’t work.

Reply

2 nixCraft November 5, 2008 at 7:00 pm

Thanks for the heads up. The faq has been updated.

Reply

3 Jon November 6, 2008 at 2:25 pm

Another option is to just install the nautilus extension, nautilus-open-terminal.

Reply

4 Dhruv March 18, 2012 at 7:46 pm

it makes nautilus crash. Using a script is a better option.

Reply

5 richard May 8, 2012 at 6:10 am

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.

Reply

6 bill November 6, 2008 at 11:13 pm

Been wanting this. Thanks!

Reply

7 Albert Bicchi November 7, 2008 at 1:22 am

K.I.S.S.

sudo aptitude install nautilus-open-terminal

Reply

8 yannis April 6, 2012 at 10:41 am

Thanks a lot !!! :)

Reply

9 Toby Haynes November 7, 2008 at 3:01 pm

Also take a look at Nautilus Actions for a GUI approach to extending Nautilus.

Reply

10 Amos Batto November 8, 2008 at 3:56 am

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.

Reply

11 Mike November 12, 2008 at 7:16 pm

Thanks! Works Perfectly in OpenSolaris Indiana

Reply

12 Frode Haugsgjerd November 26, 2008 at 8:24 pm

Nice, but why don’t you just use $PWD ?
Works fine on latest ubuntu, (8.10), as an added bonus, it’ll work on fuse mounted shares(ftp,ssh,smb…)

Reply

13 jazzyjeph January 4, 2009 at 11:20 am

Hi, have I missed something or could you not just open Synaptic and add “nautilus-open-terminal”.
Worked for me, as they say :)

Reply

14 Dennis January 18, 2009 at 9:49 am

tnx its really handy..

Reply

15 Malte February 28, 2009 at 9:56 am

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.

Reply

16 Bunkaido April 2, 2009 at 9:36 pm

Anyone know how to replicate the nautilus-open-terminal functionality but with Shutdown and Log Off options for the session?

Reply

17 Yaroslav Nikitenko December 23, 2009 at 9:28 am

Thanks.
But how can one add a menu button, or, even better, create a keyboard shortcut?..

Reply

18 netin February 1, 2010 at 3:44 pm

simply put the command ‘gnome-terminal’ in a text file inside the nautilus-script folder.set execute permission on.thats all.

Reply

19 german April 12, 2010 at 11:21 pm

Thank you so much !
sincerely, germán.

Reply

20 Bill M. April 19, 2010 at 4:01 am

Thanks so much for the great tutorial — worked like a charm on my Ubuntu system.

Reply

21 thilini July 25, 2010 at 4:42 pm

can this script modified to open different terminals, for multicore machines. as one terminal executes its all in cpu1, and the other executes all in cpu2

Reply

22 JeffH September 28, 2010 at 5:20 am

FYI, on Ubuntu 10.04.1 64-bit, I had to reboot to pick up any changes to Nautilus.

Reply

23 Cong N January 5, 2011 at 7:28 pm

You can this command nautilus -q after chances so you don’t have to reboot or logout.

Reply

24 Le Gluon du Net October 14, 2011 at 8:00 pm

Thank you very much, work immediatly on Ubuntu 11.10 with gnome 3.

Reply

25 Rahul November 4, 2011 at 8:11 am

Nice trick… thanks

Reply

26 Daniel AJ November 5, 2011 at 12:56 am

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!

Reply

27 M_P November 11, 2011 at 5:09 pm

Mega thanks.

Reply

28 Nikhil December 18, 2011 at 4:10 am

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.

Reply

29 Ahmed March 19, 2012 at 12:24 pm

Thnak you so much …

Reply

30 rich March 16, 2012 at 10:55 am

or you could just
sudo aptitude install nautilus-open-terminal

Reply

31 Adrian July 11, 2012 at 11:35 pm

@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. :)

Reply

32 Adrian July 12, 2012 at 3:23 pm

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

Reply

33 Adrian July 12, 2012 at 3:24 pm

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.

Reply

34 Adrian July 12, 2012 at 3:24 pm

…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. :)

Reply

35 Francisco April 1, 2013 at 4:05 pm

Straight to bookmarks. I have been looking for this for a long time. Thanks a lot.

Reply

36 Alex January 13, 2014 at 11:24 am

Thanks a lot! Had headaches trying to fix the spaces problem. But your shift loop method worked like a charm.

Reply

Leave a Comment

Tagged as: , , , , , ,

Previous Faq:

Next Faq: