cd Command in Linux: Change Directories

Every command you type in a terminal runs from somewhere in the filesystem, and moving to the right place is usually the first thing you do. The cd (“change directory”) command is what gets you there. It changes the current working directory in Linux and other Unix-like operating systems, and 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.
This guide explains how to use cd to move around the filesystem, from plain relative paths to symbolic links, CDPATH, and directory changes inside shell scripts.
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:
cd [OPTIONS] directoryBash accepts four options, and you will rarely need any of them:
-L- Follow symbolic links . This is the default behavior.-P- Do not follow symbolic links.cdresolves the symlink and sets your working directory to the physical (real) path rather than the symbolic path.-e- Used together with-P. If the physical working directory cannot be determined after the change,cdreturns a non-zero exit status.-@- On systems that support extended attributes, present a file with extended attributes as a directory containing them.
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:
cd Downloadscd prints nothing when it succeeds, so run pwd to confirm where you ended up:
pwd/home/username/DownloadsYou can also navigate to the same directory by using its absolute path:
cd /home/username/DownloadsIn short, if the path starts with a slash (/), it is the absolute path to the directory. The shortest absolute path of all is / itself, which takes you to the root of the filesystem:
cd /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:
cd ../
pwd/usr/localTo move two levels up to the /usr directory (the parent’s parent), you could run the following:
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:
cd ../srcThe .. moves you up to /usr/local, and src moves you back down into the sibling directory. Combining the two in a single argument saves you from typing the full path.
Navigate to the Previous Directory
To change back to the previous working directory, pass the dash (-) character as an argument to the cd command:
cd -Unlike a regular directory change, this form prints the directory it moved into:
/usr/local/shareThe shell stores that path in the OLDPWD variable, which is what cd - reads. You can inspect it at any time:
echo "$OLDPWD"Because OLDPWD holds a single path, cd - only toggles between two directories. When you need to move between more than two, pushd and popd
keep a stack of directories instead.
Navigate to the Home Directory
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:
cd ~For example, if you want to navigate to the Downloads directory, which is inside your home directory, you would type:
cd ~/DownloadsYou can also navigate to another user’s home directory using the following syntax:
cd ~usernameThe HOME environment variable holds the path to your home directory as well. Quoting it is the safer form inside scripts, where the tilde may not expand the way you expect:
cd "$HOME"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:
cd 'Dir name with space'cd Dir\ name\ with\ spaceA related case is a directory whose name begins with a dash. cd would read it as an option, so mark the end of the options with -- first:
cd -- -reportsFollowing Symbolic Links
By default cd follows symbolic links and keeps the logical path in your prompt. Suppose /var/www/current is a symlink pointing to /var/www/releases/2026-08-01:
cd /var/www/current
pwd/var/www/currentThe shell reports the path you typed rather than the directory it resolved to. Adding -P moves you through the physical directory structure instead:
cd -P /var/www/current
pwd/var/www/releases/2026-08-01The pwd -P command gives you the same answer without changing directories, which is handy when you only want to know where a symlinked path really points.
This difference shows up as soon as you move up a level. From the logical path /var/www/current, cd .. takes you to /var/www. Running cd -P .. from the same place takes you to /var/www/releases, the real parent of the resolved directory.
Using CDPATH
CDPATH lets you define a set of base directories that cd will search when you use a relative path. For example:
export CDPATH=:~/projects:/optThe leading colon is an empty entry, which stands for the current directory. With this set, cd myapp will try ./myapp, ~/projects/myapp, and /opt/myapp in that order:
cd myapp/home/username/projects/myappWhenever the match comes from a non-empty entry, cd prints the path it resolved so that you can see where you landed. Writing the current directory as . instead of an empty entry gives the same search order, but . counts as non-empty, so cd then echoes the path on ordinary local directory changes as well.
Always keep the current directory first. If you leave it out, cd somedir may quietly take you to a directory elsewhere on the system instead of the one next to you, and anything relying on relative paths will break.
To turn CDPATH off in the current shell, unset it:
unset CDPATHYou can also bypass it for a single command by clearing the variable for that invocation only:
CDPATH= cd myappUsing cd in Shell Scripts
A shell script runs in its own process, so a cd inside a script changes the directory for that script alone. Once the script exits, your interactive shell is still where it started. If you want a script to move your current shell, source it with source script.sh or . script.sh rather than running it.
Inside a script, always check that the directory change succeeded. When the directory does not exist, cd fails but the script keeps going, and every command after it runs in the wrong place:
#!/bin/bash
# Stop the script if the directory change fails
cd /var/www/mysite || exit 1
rm -rf ./cache/*|| exit 1, a failed cd leaves the script in its starting directory, and rm -rf ./cache/* deletes files from wherever the script happened to be. Guard every cd that is followed by a destructive command.A directory change made inside a subshell is discarded when the subshell ends. This is useful when a single command needs to run somewhere else without moving the rest of the script:
(cd /tmp && tar -czf backup.tar.gz data)The parentheses run cd and tar in a subshell, so the script continues from its original directory afterwards.
Troubleshooting
bash: cd: /path: No such file or directory
The path does not exist or contains a typo. Check it with ls -ld /path, and keep in mind that Linux paths are case sensitive.
bash: cd: /path: Permission denied
You need execute (x) permission on a directory to enter it. Run ls -ld /path to see the current permissions and adjust them with chmod
if the directory is yours.
bash: cd: /path: Not a directory
One of the components in the path is a regular file. This usually happens when a directory name is misspelled and the misspelling matches an existing file.
bash: cd: OLDPWD not setcd - has nothing to return to, because you have not changed directories yet in this shell session.
cd lands you somewhere unexpectedCDPATH is set and matched your argument in one of its base directories. Run echo "$CDPATH" to check, then use an absolute path or unset CDPATH.
Quick Reference
For a printable quick reference, see the cd cheatsheet .
| Task | Command |
|---|---|
| Go to home directory | cd or cd ~ |
| Go to previous directory | cd - |
| Go to parent directory | cd .. |
| Go up two levels | cd ../../ |
| Go to the root directory | cd / |
| Use an absolute path | cd /path/to/dir |
| Use a relative path | cd path/to/dir |
| Resolve symbolic links | cd -P /path/to/link |
| Enter a name starting with a dash | cd -- -name |
FAQ
What does cd - do?
It switches to the previous working directory and prints its path. The path comes from the OLDPWD variable, which holds only the last directory you left.
What is the difference between cd .. and cd ../?
They are equivalent; both move you to the parent directory. The trailing slash is optional.
What does cd / do?
It takes you to the root directory at the top of the filesystem tree. Every absolute path is measured from there.
Why does a cd inside my script not change my terminal’s directory?
The script runs in a separate process, so the change disappears when it exits. Source the script with source script.sh if you want it to apply to your current shell.
Conclusion
Relative paths and the ~, -, and .. shortcuts cover nearly all day-to-day navigation. Once you are switching between more than two directories at a time, move up to pushd and popd
and their directory stack.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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