Weekly Unix #5 - cd
#linuxman cd
No manual entry for cd
That’s because there isn’t any! cd
is an internal shell command.
So where do I find help?
Well there isn’t much in cd
that you can do except change the directory. But since this operation is done hundreds of times a day, we need to do it efficiently.
cd -
cd
with a hyphen goes to previous directory and executing that again comes back to current directory. It’s like a back button which can keep you toggling between two directories.
So next time you are switching between two paths again and again, use cd -
Bookmarking Important Locations
In a browser or in a file explorer, we can bookmark places we frequently visit. Same goes with command line. Though there is no one click solution to bookmark a location, but it’s not that hard either.
We can simply created aliases of important locations. For example, my workspace is at location ~/workspace
and I navigate there a lot! So I have added the following lines in my.bashrc
file.
alias bm_workspace="~/workspace"
So now every time, I have to go there, I simply type
cd $bm_workspace
In this example it looks like I have to type more than I would have typed earlier. But this trick is very helpful if you have a long directory path. Plus, when on terminal with auto-complete, I type $bm
and press tab
and all my bookmarks are shown for auto completion. All I have to press is w
and tab
again and I’ll be in workspace
. If you have a long path like this:
alias bm_jboss_deployments="~/toosl/jboss-7.1.0/standalone/deployments/"
alias bm_blog="~/Documents/writings/blogposts/"
aliases are quite helpful. In the above case say you want to go to Jboss Deployments, all you got to type few characters and no more!
Having a prefix like bm
helps me to list all my bookmarks at once by typing bm_
and pressing tab
if I ever forget them.
CDPATH
variable
But what if the entire directory is important. All the folders and files in a particular directory. For example my Desktop
has mostly important files and folders only and I navigate quite frequently to them. Or the user’s home
directory is important.
Here comes in hand the CDPATH
variable. CDPATH
is the base directory for cd
. Meaning - if the argument (directory you are trying to cd into) is not found in current working directory, it is searched in all the paths in CDPATH
variable.
Advantage?
Well you don’t need to type the complete directory path. You just need to type the directory you want to cd into and you will be taken there.
For example you are at home
and there is a directory called workspace
where there are multiple project folders and you have to navigate to one of the project folder. Traditionally you would navigate to the folder by typing the complete path. But with CDPATH
you just need to type the folder name.
Here’s an article on CDPATH
. It has examples and more details on the same. I’ll leave up to you to refer the said article without going into much details of CDPATH
. But it’s one important trick you shouldn’t miss!
../../../.. umm what?
At times we need to go to parent directories or grand parent or great great grand parent of the current directory. ‘..
‘ referes to parent of current directory and going to 4th parent of current directory or 5th parent is just tedious. With lots of dots and slashes and counting them, it just get’s confusing. To simplify this, I’ll share some simple aliases which if you create on your system, I’m quite sure you’ll have a hard time on navigating on some one else’s system because you will so much get used to them.
Following is the list, add to your .bashrc
and leave in peace from here onwards!
alias .='cd ..'
alias ..='cd ../..'
alias ...='cd ../../..'
alias ....='cd ../../../..'
alias .5='cd ../../../../..'
alias .6='cd ../../../../../../'
Want to go to parent?
.
Parent’s parent?
..
5th parent of current directory?
.5
You are there!! Once you start using this, you will regret why you didn’t know about this earlier!
I’ll conclude this cd
article but will keep posting more on effective navigation.
TL;DR
cd -
goes to previous directory- Use bookmarks for navigation
- Set
CDPATH
for most frequent directories - Set parent aliases