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.
Command Line · Lesson 3
cd (Change Directory)
Learn how to use cd with paths and shortcuts to move through the Linux filesystem.
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/.
Which statement correctly describes an absolute path?
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
Which command confirms the shell's current location after cd?
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 -
From /home/pete/Pictures, which command moves to /home/pete?
Which command returns to the directory used immediately before the current one?
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"
Which command treats Vacation Photos as one directory name?
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.
Lesson complete
You finished cd (Change Directory)
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.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account