This tutorial explains how to use the cd command in Linux to change the directory you are currently in, within the shell.
We’ve previously covered how to look around in directories with ls. But now you need to start navigating around the directories. We’ll do that easily with cd, change directory. Let’s start with something simple, how do we learn more about simple baked in utils like cd? If you try:
man cd
It’ll report, “No manual entry for cd”. Most likely. Instead for baked in utils, use help.
help cd
CD Syntax
Now that we’ve run our helpful help utility. We have the following output.
cd: cd [-L|[-P [-e]] [-@]] [dir] Change the shell working directory. Change the current directory to DIR. The default DIR is the value of the HOME shell variable. The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH are separated by a colon (:). A null directory name is the same as the current directory. If DIR begins with a slash (/), then CDPATH is not used. If the directory is not found, and the shell option `cdable_vars' is set, the word is assumed to be a variable name. If that variable has a value, its value is used for DIR. Options: -L force symbolic links to be followed: resolve symbolic links in DIR after processing instances of `..' -P use the physical directory structure without following symbolic links: resolve symbolic links in DIR before processing instances of `..' -e if the -P option is supplied, and the current working directory cannot be determined successfully, exit with a non-zero status -@ on systems that support it, present a file with extended attributes as a directory containing the file attributes The default is to follow symbolic links, as if `-L' were specified. `..' is processed by removing the immediately previous pathname component back to a slash or the beginning of DIR. Exit Status: Returns 0 if the directory is changed, and if $PWD is set successfully when -P is used; non-zero otherwise.
Honestly? That’s the first time I’ve looked through the cd help printout. Let’s go through the basic usages.
cd ~
The tilde is a special character that references your current home directory. When you use cd ~, it will send you to your home. If you want to know what that is:
echo $HOME
Will print out your home directory.
cd ..
The .. is also a special reference. It refers to the parent directory of your current working directory. Typing cd .. will take you to the directory above. You can add additional upward levels by typing:
cd ../..
That will proceed up twice.
cd /
The / represents the root of your system. Typing in “cd /” will take you to root if you have permissions to go there.
Conclusion
That’s it! Nice and simple working tips for using the shell.