Command Line · Lesson 3 of 19
cd (Change Directory)
0 / 5 checkpoints answered
Practice
In the practice terminal below: 1. Move to the home directory: cd /home 2. Confirm you're there: pwd pwd should print /home.
cd changes your directory; pwd shows the result.
To move around the Linux filesystem, you use paths to specify your destination. The primary tool for this is the cd command, short for change directory. It changes the shell's current working directory.
The destination must be a directory rather than a regular file. If the directory does not exist, its name is typed incorrectly, or you lack permission to enter it, cd reports an error instead of changing location.
The basic syntax is:
cd [DIRECTORY]
Understanding Paths
There are two ways to specify a path: absolute and relative.
Absolute path: The full path starting from the root directory (
/). For example:/home/pete/Desktop.Relative path: A path based on your current location. If you are in
/home/pete/Documentsand want to access a subdirectory namedtaxes, you can usetaxes/.
CheckWhich statement correctly describes an absolute path?
press 1/2/3 to answer · → next lesson
Using the cd Command
To change to a specific directory using an absolute path, type:
$ cd /home/pete/Pictures
This command moves you directly to the Pictures directory.
You can confirm your location with pwd:
$ pwd
/home/pete/Pictures
CheckWhich command confirms the shell's current location after cd?
press 1/2/3 to answer · → next lesson
Navigating to a Subdirectory
If you are already in a directory and want to move to a subdirectory, use a relative path. For instance, if your current location is /home/pete/Pictures and it contains a folder named Hawaii, you can navigate into it with:
$ cd Hawaii
Notice we only used the folder's name. This is because we were already in its parent directory, /home/pete/Pictures.
Essential Navigation Shortcuts
Navigating with full paths can be tedious. Fortunately, the shell provides several shortcuts to make moving around much faster.
.(current directory): Represents the directory you are currently in...(parent directory): Moves you one level up to the directory containing your current one.~(home directory): A shortcut to your personal home directory, like/home/pete.-(previous directory): Takes you back to the last directory you were in.
You can use these shortcuts with cd:
$ cd .
$ cd ..
$ cd ~
$ cd -
CheckFrom /home/pete/Pictures, which command moves to /home/pete?
press 1/2/3 to answer · → next lesson
CheckWhich command returns to the directory used immediately before the current one?
press 1/2/3 to answer · → next lesson
Experiment with these shortcuts to become more efficient on the command line.
Practical cd Examples
Go to your home directory:
$ cd
Running cd with no directory argument also takes you to your home directory.
Go up two levels:
$ cd ../..
Go to a directory whose name contains spaces by quoting it:
$ cd "Vacation Photos"
CheckWhich command treats Vacation Photos as one directory name?
press 1/2/3 to answer · → next lesson
Go back to the previous directory:
$ cd -
/home/pete/Documents
To reinforce your understanding of Linux directory navigation, try these hands-on labs:
- Linux cd Command: Directory Changing - Learn the Linux
cdcommand to efficiently navigate your file system, including various techniques for changing directories, understanding paths, and exploring the file structure. - Linux Directory Navigation - Put your basic Linux command-line skills to the test by navigating through directories using essential commands.
- Setting Up a New Project Structure - Practice your Linux directory management skills by creating a specific project structure and navigating through it using essential commands like
mkdirandcd.
Summary
You can now use cd to move between directories with full paths and shell shortcuts.
- Distinguish absolute paths from relative paths.
- Change directories and verify the result with
pwd. - Move to parent, home, and previous directories.
- Enter directory names that contain spaces.
- Recognize common path and permission errors.
Lesson complete
You finished cd (Change Directory). Nice work — keep the momentum going.