cd Command in Linux: Change Directories

By 

Updated on

4 min read

Linux cd Command

The cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.

The current working directory is the directory (folder) in which the user is currently working. Each time you interact with your command prompt, you are working within a directory.

cd Command

cd is a shell built-in, and its behavior may slightly differ from shell to shell. It uses the shell environment variables to determine necessary information for its execution.

We will cover the Bash-builtin version of cd.

The syntax for the cd command is as follows:

txt
cd [OPTIONS] directory

The command accepts two options that are rarely used.

  • -L, Follow symbolic links . By default, cd behaves as if the -L option is specified.
  • -P, Do not follow symbolic links. When this option is specified, cd resolves the symlink and sets your working directory to the physical (real) path rather than the symbolic path.

In its simplest form, when used without any argument, cd will take you to your home directory.

When navigating through the file system, you can use the Tab key to autocomplete the names of directories. Adding a slash at the end of the directory name is optional.

To switch to a directory, you must have executable permissions for that directory.

The pwd command allows you to find out what directory you are currently in.

Absolute and Relative Path Names

When specifying a directory to change to, you can use either absolute or relative path names. The absolute or full path starts from the system root /, and the relative path starts from your current directory.

By default, when you log into your Linux system, your current working directory is set to your home directory. Assuming that the Downloads directory exists in your home directory, you can navigate to it by using the relative path to the directory:

Terminal
cd Downloads

You can also navigate to the same directory by using its absolute path:

Terminal
cd /home/username/Downloads

In short, if the path starts with a slash (/), it is the absolute path to the directory.

The Parent Directory

On Unix-like operating systems, the current working directory is represented by a single dot (.). Two dots (..), one after the other, represent the parent directory or the directory immediately above the current one.

If you type cd ., you will change into the current directory or, in other words, the command will do nothing.

Suppose you are currently in the /usr/local/share directory. To switch to the /usr/local directory (one level up from the current directory), you would type:

Terminal
cd ../

To move two levels up to the /usr directory (the parent’s parent), you could run the following:

Terminal
cd ../../

Here is another example. Suppose you are in the /usr/local/share directory, and you want to switch to /usr/local/src. You can do that by typing:

Terminal
cd ../src

To change back to the previous working directory, pass the dash (-) character as an argument to the cd command:

Terminal
cd -

To navigate to your home directory, type cd. Another way to return directly to your home directory is to use the tilde (~) character, as shown below:

Terminal
cd ~

For example, if you want to navigate to the Downloads directory, which is inside your home directory, you would type:

Terminal
cd ~/Downloads

You can also navigate to another user’s home directory using the following syntax:

Terminal
cd ~username

Directories with Space in Their Names

If the directory you want to change to has spaces in its name, you should either surround the path with quotes or use the backslash (\) character to escape the space:

Terminal
cd 'Dir name with space'
Terminal
cd Dir\ name\ with\ space

Using CDPATH

CDPATH lets you define a set of base directories that cd will search when you use a relative path. For example:

Terminal
export CDPATH=.:~/projects:/opt

With this set, cd myapp will try ./myapp, ~/projects/myapp, and /opt/myapp in that order.

Quick Reference

For a printable quick reference, see the cd cheatsheet .

TaskCommand
Go to home directorycd or cd ~
Go to previous directorycd -
Go to parent directorycd ..
Go up two levelscd ../../
Use an absolute pathcd /path/to/dir
Use a relative pathcd path/to/dir

FAQ

Why does cd say “Permission denied”?
You need execute (x) permission on the target directory to enter it.

What does cd - do?
It switches to the previous working directory and prints its path.

What is the difference between cd .. and cd ../?
They are equivalent; both move you to the parent directory.

Conclusion

When you find yourself jumping between two directories often, look at pushd/popd for a directory stack, or set shell aliases for paths you visit daily. The $OLDPWD variable also holds the last directory cd left, which is what cd - reads.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page