70

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?

2
  • Does this help? askubuntu.com/a/75223/212231 Jul 3 '14 at 18:33
  • 4
    It might also be worth considering not doing this. The design of UNIX wasn't handed down by the forefathers for nothing you know.
    – Sean D
    Jul 3 '14 at 23:30
63

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.

4
  • 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. Jul 3 '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. Jul 4 '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 4 '14 at 10:56
  • @terdon I have seen it on Ubuntu and thought that was the general case. Jul 5 '14 at 13:58
28

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}"'
1
  • This should be selected as "the answer". Oct 1 at 21:39
6

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.

3
  • 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 1 '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. May 2 '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! 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.

2
  • 1
    The question doesn't ask how to edit .bashrc, he's already done it. And your answer is just a repeat of @terdon's answer from two years earlier. Oct 17 '18 at 11:09
  • I agree with @DavorCubranic , Shivam. You should rather transform your answer into a comment, I would say. Jun 12 '20 at 0:48
-1

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

1
  • welcome to U&L OP want a temporary change.
    – Archemar
    May 5 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.