One of the most commonly used command line tools is ‘cd’, which stands for change directory, and as you probably know is used to navigate directories and switch between one folder or another within the file system. For those just learning and starting to familiarize themselves with the Terminal and command line, here are five tricks for the otherwise simple ‘cd’ command that are guaranteed to make your life easier at the command prompt.
1: Go Back
Think of this as a back button for the command line, because it doesn’t matter what your pwd (present working directory) is, it will always take you back to where you were immediately before the present directory.
cd –
Try it out yourself, navigate to a deep folder structure then type cd – to jump back to where you were. You can repeat the command to keep going back too.
2: Go Home
You can instantly return to your home directory with the following:
cd
That command assumes your CDPATH is set to the default path of your home directory (more on that in a moment), but if it isn’t you can always rely on the tilde instead to jump back to the home directory:
cd ~
3: Go to the Parent Directory
Need to go the directory containing the current directory? That’s known as the parent directory, and you can instantly jump there by using the following command:
cd ..
Like the cd – trick, you can continuously use cd .. to keep going to the current folders parent, if you keep typing it you’ll eventually end up at the root directory.
4: Go to Root
Need to go to the very base of the file system? That’s what the forward slash is for:
cd /
Easy enough.
5: Temporarily Change the CD Path from Home to Elsewhere
Are you doing a lot of work in some deeply buried path and want that deep path to temporarily be the new default location for when you type ‘cd’? Use this:
CDPATH=/Path/To/New/Directory/Somewhere/Deep/
Doing this only changes ‘cd’ and has no impact on ‘cd ~’ for quickly navigating back to the home directory. Don’t forget to switch it back to the home directory when finished:
CDPATH=~
Rebooting typically will also switch it back to the default home directory location too, but who wants to do that if you don’t have to?
Don’t miss out on the rest of our command line tricks, we have plenty for users of all aptitude levels, whether you’re a terminal newbie or rather advanced.