85

When I open my non-login shell in Ubuntu, my present working directory is /home/user_name (my $HOME environment variable), but I want to change this such that when I start my terminal I am in some other directory.

I have read that when I start my terminal in Ubuntu a .bashrc file is sourced. So I added

export HOME=/home/user_name/Documents 

to my .bashrc file. Now, when I open my terminal I am still in /home/user_name directory.

How can I change this?

depquid
  • 3,891

5 Answers5

74

First of all, remove that line from your .bashrc. The way to do this in not by playing with $HOME, that variable will always point to your home directory and you don't want to change that just so your shells start in a different place.

I'm sure there will be a more elegant way to do this but as a temporary workaround you can simply add this line to your .bashrc:

cd ~/Documents 

Since that file is read every time you start a new non-login shell (open a new terminal), the cd command will be executed and your terminals will start at ~/Documents as you desire.

terdon
  • 242,166
  • Note that it will affect all the non-login interactive shells (and login ones if you source your bashrc from your bash_profile) and the non-interactive ones started by sshd. – Stéphane Chazelas Jul 03 '14 at 21:08
  • @StéphaneChazelas Are you sure? Usually there is a [[ -z "$PS1" ]] && exit guard at the top of the .bashrc which should prevent this for non-interactive sessions. – Martin Ueding Jul 04 '14 at 10:48
  • 2
    @queueoverflow usually? In what operating system? In which distribution? Not on Debian, SuSe or CentOS. In fact, I think I've only ever seen that on Ubuntu. – terdon Jul 04 '14 at 10:56
  • @terdon I have seen it on Ubuntu and thought that was the general case. – Martin Ueding Jul 05 '14 at 13:58
  • This changes the behavior when opening a new tab. By default, a new tab is opened in the same directory as the current tab. With this command, a new tab is opened in ~/Documents. – Jonathan Scholbach Feb 24 '23 at 15:54
  • @JonathanScholbach yes, of course. A new tab is a new shell session. I have no idea what operating system or terminal emulator you are using, there is no "default" here since most terminals don't have tabs, but that's what would be the expected behavior. If you don't want that, you shouldn't configure your shell to move to a new directory as this answer describes. – terdon Feb 24 '23 at 17:38
  • @terdon: Thanks for your response! I use Ubuntu and bash. Do you know how I could start a new terminal session in my custom folder, but stay in the current directory when I open a new tab? (I don't want to change my root dir in /etc/passwd for this). I can open a new question for this as well, if this is preferred. – Jonathan Scholbach Feb 24 '23 at 21:31
  • @JonathanScholbach yes, please open a new question but make sure to explain why you want this since a tab is the same as a new terminal. Also make sure to mention what terminal program you use. – terdon Feb 25 '23 at 10:56
  • @terdon: Do you think, I should ask this here, or on askubuntu? – Jonathan Scholbach Feb 25 '23 at 16:06
  • @JonathanScholbach up to you, it is completely on topic on both sites. – terdon Feb 26 '23 at 11:49
  • @terdon Thanks again, your comments already helped me asking a better question. It is here: https://unix.stackexchange.com/questions/736983/change-default-directory-in-gnome-bash-terminal – Jonathan Scholbach Feb 26 '23 at 12:41
33

The terminal will start in the working directory it inherits from its parent.

However, some allow to override it via configuration settings.

With gnome-terminal, you can edit your profile, tick run a custom command instead of my shell and make it:

sh -c 'cd ~/Documents; exec "${SHELL:-sh}"'
10

Here's what I put in my ~/.bash_profile:

alias curDir="pwd | sed -e 's/ /\\ /'"
alias save='echo `curDir` > ~/.saved_dir'
alias saved='cd `cat ~/.saved_dir`'
saved

and I rely on this all the time. Basically, when I'm working on some project, I'll save a main directory for it and then every subsequent session starts there. Then when I change projects or whatever, I'll just save somewhere else.

I found this question when looking for a better way, but what I've got is preferable to me than hardcoding one particular directory.

amos
  • 201
  • This works well in git bash as well. Just remember to restart the bash shell or use (I think) "source ~/.bash_profile" after changing the file. – Tim Aug 01 '19 at 18:17
  • @amos Why not use a soft link? It seems like a more canonical way to do what you're doing. Either way, this is a nice idea. – Jonathan Lam May 02 '20 at 18:32
  • Just came across this, very simple but yet such a nice feature to have. Using it right now. Thanks for sharing! – Rens Tillmann May 18 '20 at 19:04
1

For those that are looking at where the .bashrc file is, generally it is a hidden file in home directory.

To find it:

Open your terminal and go to the home directory, and then:

ls -al

to show the all hidden files. Now you will be able to see your .bashrc file.

In order to change default directory

  1. Open .bashrc file in your text editor by entering the command:

    gedit .bashrc
    
  2. Edit this file by adding your command in last, like:

    cd ~/YourDirectoryName
    
  3. Save the file and restart the terminal.

eedrah
  • 103
Shivam
  • 19
  • 2
-1

Think about modifying /etc/passwd and change your default home directory