Setting default path when opening a terminal session

78,183

Solution 1

Just run the following command in your terminal:

echo "cd ~/Desktop/Java\ Files" >> ~/.bashrc

The above command will add a new line in your ~/.bashrc file that contain cd ~/Desktop/Java\ Files and that will change your default working directory to /home/Varun/Desktop/Java Files when you will open the terminal.

Reopen the terminal and you will see the difference.

Solution 2

You can add the following line to the end of your ~/.bashrc

cd ~/Desktop/Java\ Files

Solution 3

Although changing the $HOME variable and calling cd command (i'll use cmd for short) in .bashrc file is right answer to your question,

i find it more comfortable to create alias (for example cdh) which takes me directly to directory i want.

The reason is that all files which configures other programs (Just like .bashrc for example) stay in default $HOME directory and i can work in my "cdh directory" without interuption from theese files.

If i needed to go back to $HOME directory i can allways use cd cmd.

In some linux distros the .bashrc file is shipped with command or commands which runs or run one or multiple other files intended for that specific use (for example .bash_aliases)

so decide for yourself if you want to use them or not,

in case you want to use them, just use it the same as you use .bashrc but with commands inteded for the specified file.

so in .bashrc (or in .bash_aliases or whichever file you've chosen)

write following:

alias cdh='cd /home/Varun/Desktop/Java Files'

if you don't like cdh alias don't be afraid to use different name but make sure there isn't any other cmd or alias named like this, couse you could make that cmd more or less unusable.

You can check if the name is taken by triyng to call it but i would sugest a type cmd with argument of name of another cmd.

The type cmd should tell you if the given cmd is alias, binary file, or bash script, or ......... whatever. And therefore will tell you when cmd doesn't exist. (Which is what you want in this case)

Solution 4

Add this to your ~/.bashrc file. If you open terminal from nautilus in your home directory, it will unfortunately still cd. I'm not sure how escaping works, but this might not work if your current directory or home directory has unescaped characters.

if [ "$PWD" == "$HOME" ]; then cd Desktop/Java\ Files/; fi

Solution 5

While putting cd ~/Desktop/Java\ Files into the .bashrc file does work, it has the terrible side-effect of overriding the Nautilus Open in Terminal action, so you can no longer open any other sub-folder in a terminal.

My solution is to change the Keyboard Shortcut in Settings for Ctrl+Alt+T to run this command:

/usr/bin/gnome-terminal --working-directory="/home/butch/Documents" 

This will open new terminal windows in my Documents folder, while allowing Nautilus to open new terminal windows wherever.

Note this command does not accept ~/Documents, the full path must be specified.

Share:
78,183

Related videos on Youtube

UnderDog
Author by

UnderDog

Coding for Food

Updated on September 18, 2022

Comments

  • UnderDog
    UnderDog almost 2 years

    I am new to Ubuntu.

    Whenever I open a terminal my current working directory /home/Varun (as found by typing pwd).

    Is there any way I can make the terminal's path to be set as /home/Varun/Desktop/Java Files when it opens?

  • Tomáš Růžička
    Tomáš Růžička over 5 years
    Sorry if i made any mistake. Im not a native english speaker.
  • Heisenberg
    Heisenberg about 4 years
    But whenever you need open through a file manager, it'll still open the path you gave in ~/.bashrc file. So, this is not the best option.
  • Dan Ortega
    Dan Ortega over 2 years
    Awesome. Thanks