How to Define the Base Directory for the “cd” Command in Linux

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 1

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.

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 2

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

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 3

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/

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 4

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.

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 5

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

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 6

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

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 7

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”.

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 8

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

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 9

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.

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 10

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

how-to-define-the-base-directory-for-the-cd-command-in-linux photo 11

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.

Article How to Define the Base Directory for the “cd” Command in Linux compiled by Original article here

More stories

Quick Tip: Place Your iPhone Face Down to Save Battery Life

iOS 9 brought a useful new feature you may not have noticed yet. Known as “Facedown detection”, your iPhone can detect when it’s placed face down and won’t turn the screen on when notifications arrive. This can save a lot of battery power if you regularly receive notifications.

How to Use Twitter on the Amazon Echo

You can already do a whole lot with your Amazon Echo, but now you can have Alexa read out tweets from your Twitter timeline and more. Here’s how to use Twitter on the Amazon Echo.

How to Fix Kodi’s YouTube “Quota Exceeded” Problem

Are you sick of Kodi’s YouTube plugin constantly crashing, and giving you cryptic error messages like “Exception in ContentProvider” and “Quota Exceeded”? Here’s how to fix that problem with just a few minutes of work.

How to Turn Any Web Page Into a Web App on a Chromebook

Chrome OS has long been more than “just a browser.” Since it essentially requires an active internet connection for most activity, web apps are the backbone of the Chrome OS ecosystem—but did you know that you can actually turn any page into a its own web app, launchable from the taskbar? Here’s

The Fastest Ways to Rename Files on macOS

Most people rename files by clicking on a file, waiting, and clicking on the file name again. But macOS has quite a few great ways to rename files, some of which are even faster.

How to Use Scribble to Write Messages on Your Apple Watch

In the earlier days of the Apple Watch, when you needed to enter text, you had to either use a canned response, an emoji, a doodle, or speak your message aloud and hope the watch would transcribe it correctly. However, that has changed with watchOS 3.

How to Stop Apple Pay from Opening on Your iPhone All the Time

If you don’t use Apple Pay, there’s no reason you should have to see it on your iPhone’s lock screen every time you accidentally double-click the Home button. Here’s how to turn that shortcut off, while still allowing access to Apple Pay when you need to.