26

Whenever I start my console gnome-terminal in Ubuntu, it starts in the home directory. How can I make it start in a different directory say ~/myfolder? I tried to write cd ~/myfolder in ~/.profile but nothing happens.

Ken
  • 77,016
  • 30
  • 84
  • 101
Vikrant Chaudhary
  • 11,089
  • 10
  • 53
  • 68

10 Answers10

49

If you start gnome-terminal like gnome-terminal --working-directory=myfolder it will start with the working directory at ~/myfolder so you could add a new entry to your menu to use that command instead of the other one.

Cdsboy
  • 740
  • 7
  • 11
  • Also see another (better) solution - http://stackoverflow.com/questions/844677/gemone-terminal-how-to-start-in-a-different-directory/844711#844711 – Vikrant Chaudhary May 10 '09 at 05:12
  • 7
    Note that in Krusader terminal settings this should be without "=" so this should work: `gnome-terminal --working-directory %d`. – Nux Oct 30 '12 at 20:43
  • 1
    This no longer works in Ubuntu 16.04, due to a bug: https://bugs.launchpad.net/ubuntu/+source/gnome-terminal/+bug/1587154 Please go there and click 'this affects me too' if you'd like it fixed. – Jonathan Hartley Sep 14 '16 at 20:50
13

I did this way - with script: open 3 tabs in the same window size 170x40, each "tab" starts in a different directory.

gnome-terminal --geometry=170x40 --working-directory=myfolder1 \

--tab --working-directory=myfolder2 \

--tab --working-directory=myfolder3 
  • Most general solution imo. Exactly what I was looking for. Thank you! – user643011 Apr 02 '12 at 21:02
  • 1
    This no longer works in Ubuntu 16.04, due to a bug: https://bugs.launchpad.net/ubuntu/+source/gnome-terminal/+bug/1587154 Please go there and click 'this affects me too' if you'd like it fixed. – Jonathan Hartley Sep 14 '16 at 20:50
12

Add the following to your ~/.bashrc

cd ~/myfolder
mdec
  • 5,122
  • 4
  • 25
  • 26
  • It worked too along with this (link below), but this one is a better solution I guess. http://stackoverflow.com/questions/844677/gemone-terminal-how-to-start-in-a-different-directory/844704#844704 – Vikrant Chaudhary May 10 '09 at 05:10
  • from "this is a better solution", I mean adding to ~/.bashrc – Vikrant Chaudhary May 10 '09 at 05:11
  • 2
    There is a caveat with ~/.bashrc i.e., if you open a new tab in terminal, you go back to ~/myfolder, which is usually undesirable. so now I think gnome-terminal --working-directory=myfolder is rather a better solution. – Vikrant Chaudhary May 13 '09 at 07:23
  • 4
    This is not how bashrc is meant to be used. If you do this, it will affect every interactive shell you run, not just the ones that run in gnome-terminal. If you want to control gnome-terminal, then set something that affects just gnome-terminal. – Neil Mayhew Jul 17 '10 at 23:31
5

Directory option

There is the option --working-directory to specify the startup directory of the terminal (no short option form).

The basic approach to open the terminal in /some/dir is

gnome-terminal --working-directory=/some/dir

but there is a trap...

Bad trap

Assuming we want to start the terminal in the directory ~/dir.

This does not work:

gnome-terminal --working-directory=~/dir

The command looks perfectly fine according to the option syntax, but the terminal starts in the home directory.
It's because it does not expand the tilde (~), for confusing reasons - see below.

Thesse do work:

gnome-terminal --working-directory=/home/auser/dir

gnome-terminal --working-directory=$HOME/dir

gnome-terminal --working-directory ~/dir

Tilde expansion

Note there is no = in the last variant. Because of this, the ~ is at the start of a shell word, and therefore is handeled by tilde expansion.

The problem is that ~ does not get expanded everywhere, but only in certain places. One of them is in variable assignments, like directory=~/dir. That's ok, ~ gets expanded to $HOME, but --working-directory=~/dir does not expand ~, because that is not a variable assignment, it only looks very similar.

Community
  • 1
  • 1
Volker Siegel
  • 3,277
  • 2
  • 24
  • 35
4

You could use the nautilus-open-terminal extension. This allows you to right-click on a folder in nautilus and open a terminal window with that directory as its working directory.

You can also run a terminal in the normal way, type "cd ", and drag a folder icon from nautilus to the window. This will paste the path of the folder into the command line and you then type return to change to that directory. You can do the same thing with regular files to paste their path and run commands on them.

Neil Mayhew
  • 14,206
  • 3
  • 33
  • 25
3

Actually, this is how I turn it off for everyone by default.

gconftool-2 --direct \
--config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory/ \
--set -- type=bool /apps/nautilus-open-terminal/desktop_opens_home_dir true
Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66
1

If the folder has a complicated path, ie not just ~/myfolder, you could create a symlink to it in your home directory so you can get to it quickly. You can also set the CDPATH environment variable to tell bash to search a list of directories when you type cd myfolder.

Neil Mayhew
  • 14,206
  • 3
  • 33
  • 25
1

I'm impressed by Neil's Mayhew comment and Volker's Siegel answer. I've tried to not only set default directory for gnome-terminal but preserve habitual behavior of desktop environment as well (I'm using Linux Mint 17.1 Cinnamon, GNOME Terminal 3.6.2, perhaps it also can be applied for other Gnome-congenered DEs). So let me put my two cents in.

  1. Adding cd ~/myfolder at the very end of ~/.bashrc does the job. But as already mentioned it will affect every interactive shell. Even more, if you open some directory in a file manager (Nemo or Nautilus or something like this) and appeal to the context menu from there (e.g. right click and then select Open in Terminal) new instance of gnome-terminal will be started in ~/myfolder regardless of the folder which was loaded in the file manager. Even if you run gnome-terminal --working-directory=/some/other/folder explicitly it will still open ~/myfolder. Seems that the approach with .bashrc is unusable.

  2. gnome-terminal --working-directory=myfolder works fine but only when you use custom menu entry in you DE (or custom shortcut on desktop) which runs terminal with this parameter. If you would like to run gnome-terminal from command line or from mini-launcher (press Alt + F2), you have to type the parameter every time. Anyway this approach is more-or-less usable.


How gnome-terminal determines which folder to open? When --working-directory is not specified it opens current working directory (e.g. $PWD) otherwise it opens directory specified explicitly.

I've found the following solution.

Create a file named gnome-terminal in your ~/bin folder. It will act as shortcut but from everywhere (start menu, mini-launcher, other terminal instance, etc) because ~/bin is already in $PATH (at least in Linux Mint...). Make this file executable. Then put the following content into the script:

#!/bin/bash

home_directory=~

if [ "$PWD" == "$home_directory" ]; then
    # When 'gnome-terminal' was ran from either 
    #     - start menu
    #     - mini-launcher
    #     ... 
    # parent directory is set to $HOME.
    #
    # We respect original command line arguments. 
    # For example, when terminal is ran from another 
    # terminal instance and '--working-directory' is 
    # specified explicitly we should left it as it is.  
    # If there are two '--working-directory' switches 
    # in the command line 'gnome-terminal' will pick up 
    # the last one.
    #
    # Also we use full path to executable here in order
    # to prevent recursive calling of 'gnome-terminal' from 
    # '~/bin'.
    /usr/bin/gnome-terminal --working-directory=/ "$@" 
else
    # 'gnome-terminal' was from another directory. 
    # We don't change anything.
    /usr/bin/gnome-terminal "$@"
fi

If you run gnome-terminal from start menu you will see that current folder in new terminal is / (you can use any folder, for example, ~/myfolder because our custom wrapper is a Bash-script, so shell's expansion with work fine).

If you appeal to Open in Terminal in a file manager you will get current directory in the new terminal.

If you run gnome-terminal with explicit --working-directory parameter (perhaps, from existing terminal instance) new terminal instance will be opened in the directory you specified.

Community
  • 1
  • 1
flaz14
  • 826
  • 1
  • 13
  • 25
1

To open in my desired directory as root user I ran:

gnome-terminal --working-directory=/home/my-project/ -x bash -c "sudo su"
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
1

From GNOME Terminal - Getting Started:

You can also specify a command that runs automatically when you start GNOME Terminal in the profile.

lothar
  • 19,853
  • 5
  • 45
  • 59