cd Command in Linux with Examples

In Linux, the “cd command” is pivotal for navigating the open-source operating system’s vast filesystem. This seemingly simple command, standing for “change directory,” is a cornerstone of user navigation and file management, showcasing the depth and power of Linux’s command-line utilities.

Understanding the cd Command

What is the cd Command?

In Linux, the cd command serves to change the current working directory. As one of the most frequently used commands, it’s a foundational tool for any user. As you work within the terminal, shifting between directories becomes a common task, and cd ensures this transition is effortless.

Why use the cd Command?

  • Efficiency: Bypassing the need to use a graphical interface to sift through folders, the cd command provides a more rapid and direct access to nested directory structures.
  • Scripting: For shell scripting, the cd command facilitates automated tasks to transition between different directories, amplifying the potential of automation.
  • Integration with Other Commands: Working in tandem with other Linux commands, cd provides a streamlined command-line experience.

Syntax of the cd Command

The elementary syntax of the cd command is:

cd [directory]

Here, [directory] represents the name or path of your desired directory.

Practical Examples of the cd Command

Navigating to a Specific Directory

To navigate directly to a specific directory, provide the path of the directory you want to access.

cd /var/www/html

This command takes you to the web root folder typically used for web applications on Linux systems.

Quick Home Directory Access with Tilde (~)

The tilde (~) character is a shortcut for your home directory, making it convenient to move to folders within your home directory.

cd ~/Downloads

This action transports you directly into your Downloads folder located within the home directory.

Ascending Multiple Directory Levels with Double Dots (..)

You can move up directory levels using the .. notation.

cd ../../

This command lets you move two directory levels up from your present position.

Accessing Hidden Directories

Hidden directories in Linux start with a dot (.). Although not visible by default, navigating into them is straightforward.

cd .config

By executing this, you would enter the .config directory, a common hidden directory in user home folders.

Working with Directories Containing Spaces

If a directory name has spaces, enclose the name in quotes or use a backslash before the space.

cd "My Documents"


cd My\ Documents

Both commands would effectively get you into the “My Documents” directory.

Deeper Navigation into Subdirectories

For efficient navigation, specify complete relative paths to move deeper into subdirectories.

cd Documents/Work/Projects

With this command, you directly enter the Projects subdirectory inside Work, which is within Documents.

Utilizing Bash Auto-Completion

Bash provides auto-completion for faster directory navigation. Begin typing a directory name and press Tab to complete it.

cd Doc[Tab]

Assuming a Documents directory exists, it auto-completes the name.

Returning Home

No matter where you are in the directory structure, you can instantly return to the home directory.


This always lands you back in your user’s home directory.

Accessing the Root Directory

The topmost directory in Linux is the root directory. To move to it:

cd /

This command transports you to the root of your file system.

Switching to Another User’s Home Directory

If you have permission, navigate to another user’s home directory.

cd ~username

Replace username with the desired user’s name.

Directories with Dashes in Their Name

Navigate to them just like any directory.

cd tech-archive

This takes you inside the tech-archive directory.

Advanced Examples of the cd Command in Linux

Old Directory Switching with Dash (-)

The cd command, when used with a dash (-), switches between the current directory and the previous directory. This functionality is helpful when you’re working between two directories frequently.

cd -

Upon executing, you’ll find yourself in the directory where you were just before the last cd command.

Creating and Navigating Simultaneously with mkdir -p and cd

A combination of commands can facilitate creating nested directories and navigating into them instantly.

mkdir -p Documents/NewProject/Module1 && cd $_

This sequence first generates the nested directories Documents/NewProject/Module1 and then directly navigates into Module1.

Using Variables for Directory Navigation

Shell variables can store directory paths. This is useful for directories you access frequently but don’t want to type out the full path each time.

cd $mydir

The above commands store the directory path in the variable mydir and then navigate to it using the cd command.

Navigating with Command Substitution

This method uses the output of one command as the input for another, offering dynamic navigation.

cd $(dirname $(find / -name "testfile.txt" 2>/dev/null))

Here, the find command searches for “testfile.txt”, and dirname extracts its directory. cd then navigates to that directory.

Pushd and Popd for Directory Stacking

The pushd and popd commands work in tandem with the cd command for directory stacking, effectively maintaining a stack of directories to jump between.

pushd ~/Documents
pushd ~/Downloads

Initially, pushd navigates to ~/Documents. The next pushd switches to ~/Downloads, but remembers ~/Documents. Finally, popd returns to the last directory in the stack (~/Documents).


The cd command, central to Linux navigation, offers more than just simple directory switching. By understanding its versatility, paired with other command line tools and techniques, users can significantly optimize their navigation efficiency and speed. Whether you’re a newcomer or an experienced Linux enthusiast, mastering the cd command is an indispensable skill in your Linux journey.