Easy Navigation Through Your Folders
Forget the parent directory, if you want to go to a sub-directory just type ‘cd subdirectory’ in the terminal (without quotation marks). But how does this work?
The trick here is the CDPATH environment variable, which is a variable that works only with the cd command. It is just like PATH environment variable, but the difference between PATH and CDPATH is that PATH is usable with every command. Before using the CDPATH variable, you have to give values to it because it is empty by default.
How do you display the value of the CDPATH environment variable? Type ‘echo $CDPATH‘ exactly as shown in Figure 1.
My current CDPATH is /home/oltjano/Desktop/blog. Now, if I want to navigate to a sub-directory inside blog, I just type the cd command followed by the name of the subdirectory in the blog directory. Ok, if you are new to this and you still don’t believe this cool hack will work, let me show you my working directory by typing ‘pwd‘ in the terminal like shown in Figure 2.
Figure 2 shows my working directory. It is /home/oltjano. Now if I want to cd to any sub-directory inside blog directory I have to type the full path name like shown in Figure 3.
It took me 2.5 seconds to type the full pathname so I can change directory to mail. I have to perform this task often and it is really boring and time consuming, so CDPATH hack is my solution. What if I make mistakes while typing the all path name? The mistake will take my precious time and I will get nervous if this happens to many times. Now let me perform the hack. Watch geeks! It is very easy and very beautiful. I just type ‘cd mail‘ as shown in Figure 4 and it works like a charm.
If I type ‘pwd‘ in terminal it will show that /home/oltjano is not anymore my working directory, it is replaced by /home/oltjano/Desktop/blog/mail.
Did you see me type the full path-name? No, you did not. I suggest everyone to use this cool hack to navigate easily through folders. You can add other path names to CDPATH variable by separating each path name with colon.