By default, the Terminal window in Linux opens to your home directory. To change to any directory that is not directly in the home directory, you must provide the full path or use the “cd” command multiple times.

For example, I regularly work with multiple directories within the Documents directory, which is in the home directory. I would like to be able to cd to folders in the Documents directory without having to type

        cd Documents
    

first (or providing the full file path).

Sound confusing? Here's an example. Let's say I have a folder called htgarticles inside my Documents folder, and I want to access it through the terminal.

We'll start by opening a Terminal window and entering the

        pwd
    

(Print Working Directory) command to show that the current directory is indeed our home directory, /home/lori.

01_running_pwd_command

If we type the following command, we get an error saying that there is no such file or directory. That’s because we are not in the Documents directory.

cd htgaricles

02_no_such_file_or_directory

To get to the htgarticles directory, first we have to change to the Documents directory.

cd Documents/

Then, we have to change to the htgarticles directory.

cd htgarticles/

03_changing_directories

Alternatively, we could arduously type out the full file path:

cd ~/Documents/htgarticles

It doesn't have to be this way, though. We can make any directory the base directory for the Terminal window, so we can cd to its sub-directories without typing the full path.

In our example, we want to set the Documents directory as the base directory, because all the directories I work with--like htgarticles--are inside the Documents directory. So, we type the following command at the prompt and press Enter.

export CDPATH=~/Documents/

NOTE: In the above command, export must be all lowercase and CDPATH must be all uppercase. Replace ~/Documents/ with the path to whichever parent directory contains the subdirectories you work with most often.

04_export_cdpath_command

Now, still in our home folder, we can run cd htgarticles and go straight to ~/Documents/htgarticles.

05_changing_directory_now_works

If you want to automatically set a certain directory as the base directory every time you open a Terminal window, you can do that by editing the .bashrc file. The .bashrc file is a script that runs every time you open a Terminal window, and you can add whatever commands you want to it. So, we can add the export CDPATH command to set a specific directory as the parent directory for every Terminal window.

To do this, cd back to your home directory if you aren't already. Then, type the following command to open the .bashrc file. You can use whichever text editor you want to use, but we’re going to use gedit in our example.

gedit .bashrc

06_opening_bashrc_file

Scroll to the bottom of the .bashrc file and add the following command.

export CDPATH=~/Documents/

Again, replace ~/Documents/ with the parent directory that contains the subdirectories you work with most.

You can add a comment above the command so you remember what the command does. Simply put a number sign (#) at the beginning of the line, and then any description you want to add.

Click “Save”.

07_adding_export_command_to_bashrc_file

Close gedit (or whatever text editor you're using) by clicking the “X” in the upper-left corner of the window.

08_closing_gedit

The command you just added to the .bashrc file will not affect the current Terminal window session. You must close the current Terminal window and open a new one. So, type exit at the prompt and press Enter or click the “X” button in the upper-left corner of the window. Then, press Ctrl+Alt+T to open a new Terminal window.

09_closing_terminal_window

Now, even though the current working directory is the home directory, you can directly change to the subdirectories from the base directory you chose.

10_base_directory_changed

Related: How to Open the Terminal to a Specific Directory in Linux

If you want to revert back to the home directory as the parent directory in the Terminal window, simply open the .bashrc file in a text editor and either delete the command you added or comment it out by adding a pound sign (#) at the beginning of the line. Commenting out the line is useful if you want to activate the command again in the future. You can also easily change which directory you want to use as the base directory simply by changing the directory in the command in the .bashrc file and saving the file.

If you have one specific directory you work in most of the time, you can also add a command to the .bashrc file that will open the Terminal window to that directory every time.