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.
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.
To get to the htgarticles directory, first we have to change to the Documents directory.
Then, we have to change to the htgarticles directory.
Alternatively, we could arduously type out the full file path:
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.
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.
Now, still in our home folder, we can run
cd htgarticles and go straight to ~/Documents/htgarticles.
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.
Scroll to the bottom of the .bashrc file and add the following command.
~/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.
Close gedit (or whatever text editor you’re using) by clicking the “X” in the upper-left corner of the window.
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.
Now, even though the current working directory is the home directory, you can directly change to the subdirectories from the base directory you chose.
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.
- › How to Delete a BeReal Post
- › Apple Will Allow End-To-End Encrypted iCloud Backups
- › Google Chrome Will Upgrade Page Links for Better Security
- › Uber’s Las Vegas Robotaxies Still Need Chaperones (For Now)
- › How to Update Audio Drivers on Windows
- › Save Big on an Android TV Projector, Portable SSD, and More