Working in Terminal command line you’ll often need to change directory. For this we use the cd command. The cd command has various options that alter what it does. I’ll list the five most useful cd command options here.
1. Go Home
First off is cd on its own.
Without anything after it cd will take you to your home directory. Your home directory is:
In my case it is:
When using cd on its own, the home directory is called from the system variable CDPATH. We’ll look at that in a moment. If your CDPATH does not point to your home directory (because you temporarily changed it), the failsafe command to go to the home directory is:
The ~ symbol can also be utilised in Finder as a reference to the home directory, for instance, when you’re using the “Go to folder…” command in Finder. So it’s a handy little symbol to remember.
2. Go Back
A bit like the back button in your web browser, this command will take you back to the previous directory you were working in.
Simple, and yet it can be very handy.
A quick aside: It may help to know that the directory you are currently working in is referred to as the pwd, which stands for Present Working Directory. If you issue the command pwd into Terminal it will display the path of the directory you are presently working in. That can be handy for a few things. One is that you can actually copy and paste commands and paths (etc.) within Mac OS X Terminal. That can save you having to type out long paths.
To copy your present path just type in pwd, then highlight the path that is displayed (using the mouse pointer), hit Ctrl-C, and then to paste it into a command use Ctrl-V when you have the cursor in the position you wish to paste into. Below is an example of what comes up with the pwd command.
J-MBP:~ jonathan$ pwd /Users/jonathan J-MBP:~ jonathan$
3. Up to Parent Directory
This command will change the active directory to the Parent Directory, which is the level above whatever directory you are working in.
This is starting to sound like a family reunion! Take me home, and go to the parent directory. Whatever next? If it helps, you can think of the two dots are the mother and father of the pwd. So typing this command takes up up to the parent directory. 🙂
4. Getting to the Root of the matter
Next is changing to the root directory. That’s the highest (or lowest, depending on your perspective) root or base of the entire system. All drives, folders, etc., are on levels after the root.
5. Changing the CDPATH variable (temporarily)
If you’re doing something on a folder that is buried deep in the system, with a long path, you’ll hardly want to be typing that path out each time you reference it. To save some time you can simply make a temporary change to the CDPATH variable. It will revert back to the default (your Home directory) the next time you reboot the system. You can also manually change it back.
And to change it back to the home directory, simply type in this:
Thanks about it for now. If you are interested in learning more about Terminal, and/or if you found this post useful, please leave a comment saying so.