cd Command: Change Directories in Linux

Use the cd command to navigate Linux directories. Covers shortcuts, absolute and relative paths, CDPATH, symlinks, and error fixes.

Last updatedAuthorJoshua JamesRead time8 minGuide typeLinux Commands

Changing directories in Linux is more predictable once you understand what the shell does with paths before cd runs. The cd command in Linux changes the current working directory for the shell session you are already using, so it sits behind nearly every terminal workflow, from opening project folders to checking logs or returning home.

The examples use Bash-style output and a disposable ~/cd-demo practice tree where predictable paths matter. Basic navigation forms such as cd, cd .., cd -, quoted paths, and tilde expansion work in common Linux shells, while tab completion, CDPATH, and directory-stack behavior can vary by shell.

Understand the cd Command in Linux

Unlike many Linux commands, cd is a shell built-in rather than a standalone program. It must run inside the current shell because an external program cannot change the directory of the terminal session that launched it. That is also why sudo cd /root does not work as a normal command; cd has to run in the shell whose location you want to change.

cd Command Syntax and Options

The general cd syntax accepts an optional option and an optional destination:

cd [option] [directory]

Running cd with no directory returns to the path stored in HOME. The destination can be an absolute path, a relative path, a tilde shortcut, or a directory found through CDPATH. Bash also supports these common options:

  • -L keeps the logical path, which is the default behavior when a symbolic link is involved.
  • -P resolves symbolic links and changes to the physical directory path on disk.
  • -e can be used with -P in Bash so cd fails if the physical current directory cannot be determined.

The OLDPWD variable stores the previous working directory for cd -. The PWD variable stores the shell’s current working directory, and CDPATH can define search locations for relative directory names.

Verify cd Is Available

Since cd is built into the shell, it does not require installation. Confirm that your shell provides it with type:

type cd

Expected Bash output:

cd is a shell builtin

Quick Reference: cd Command Shortcuts

Use these cd patterns as a quick lookup when you need to move around the filesystem:

TaskCommandWhat It Does
Home directorycd or cd ~Returns to your home directory
Previous directorycd -Switches to the last visited directory
Parent directorycd ..Moves up one level
Up two levelscd ../..Moves up two directory levels
Root directorycd /Navigates to the filesystem root
Absolute pathcd /var/logNavigates to an exact filesystem path
Relative pathcd projects/webappNavigates relative to your current location
Spaces in a namecd "My Folder"Handles directory names containing spaces
Another user’s homecd ~usernameExpands to another account’s home directory when permissions allow it

The safest way to practice cd patterns is inside a directory you own. Create a small demo tree first, then run the examples from that location. The examples use pwd, short for print working directory, to show where the shell landed.

mkdir -p ~/cd-demo/Documents ~/cd-demo/projects/webapp ~/cd-demo/logs/nginx ~/cd-demo/logs/archive ~/cd-demo/"My Documents"
touch ~/cd-demo/logs/access.log ~/cd-demo/logs/error.log ~/cd-demo/logs/system.log
cd ~/cd-demo
pwd

Example output:

/home/user/cd-demo

Change to a Specific Directory

Pass a directory name to cd to move into it. This example starts from ~/cd-demo and enters the Documents directory:

cd Documents
pwd

Example output:

/home/user/cd-demo/Documents

Change Directory and List Contents

Chain cd with another command when the second action should run only after the directory change succeeds. The && operator stops ls from running if cd fails:

cd ~/cd-demo/logs && ls -1

Example output:

access.log
archive
error.log
nginx
system.log

Navigate Using an Absolute Path

An absolute path starts from the filesystem root, which is /. The $HOME variable expands to your home directory, so this command becomes an absolute path before cd runs:

cd "$HOME/cd-demo/logs"
pwd

Example output:

/home/user/cd-demo/logs

Navigate Using a Relative Path

A relative path starts from your current directory. Return to ~/cd-demo, then enter a nested project path without typing the full location:

cd ~/cd-demo
cd projects/webapp
pwd

Example output:

/home/user/cd-demo/projects/webapp

The shell resolves projects/webapp relative to ~/cd-demo. If you start somewhere else, the same relative path points somewhere else or fails.

Return to the Previous Directory with cd –

The cd - shortcut switches to the directory stored in OLDPWD. Bash prints the destination path when the switch succeeds:

cd ~/cd-demo/logs
cd ~/cd-demo/projects
cd -

Expected output:

/home/user/cd-demo/logs

Running cd - repeatedly toggles between two directories, which is useful when you need to move between related locations.

Move to the Parent Directory with cd ..

The .. entry refers to the parent of the current directory. Use it to move up one level:

cd ~/cd-demo/logs/nginx
cd ..
pwd

Example output:

/home/user/cd-demo/logs

Move Up Multiple Directory Levels

Chain .. entries with slashes to move up several levels at once:

cd ~/cd-demo/logs/nginx
cd ../..
pwd

Example output:

/home/user/cd-demo

Each .. moves up one level. The pattern ../../.. would move up three directory levels.

Navigate to the Root Directory

The root directory, written as /, is the top of the Linux filesystem hierarchy. Every absolute path begins there:

cd /
pwd

Expected output:

/

Return to the Home Directory

Run cd without an argument to return to your home directory. The ~ shortcut points to the same location and can be combined with longer paths:

cd
pwd

Example output:

/home/user
cd ~/cd-demo/Documents
pwd

Example output:

/home/user/cd-demo/Documents

Access Another User’s Home Directory

A tilde followed by a username expands to that account’s home directory. Replace username with a real local account:

cd ~username

This only works when the account exists and your user has permission to enter that home directory. If the shell cannot expand the name, it leaves the text unchanged and cd usually reports that the directory does not exist.

Handle Directory Names with Spaces

Wrap directory names that contain spaces in quotes, or escape each space with a backslash. Without quoting or escaping, Bash treats each word as a separate argument:

cd ~/cd-demo
cd "My Documents"
pwd

Example output:

/home/user/cd-demo/My Documents

The backslash escape method reaches the same directory:

cd ~/cd-demo
cd My\ Documents
pwd

Example output:

/home/user/cd-demo/My Documents

Navigate Nested Subdirectories

Provide multiple directory levels separated by slashes to jump directly into a nested subdirectory:

cd ~/cd-demo/logs/archive
pwd

Example output:

/home/user/cd-demo/logs/archive

This works with absolute and relative paths. To create directories with the mkdir command before navigating, use mkdir -p path/to/new/dir && cd path/to/new/dir.

Use Tab Autocomplete with cd

Type the first few characters of a directory name and press the Tab key. The shell completes the name when there is one match; when several entries share the same prefix, pressing Tab twice usually lists the choices:

cd ~/cd-demo/lo<Tab>

With the demo tree above, Bash completes that path to cd ~/cd-demo/logs/. Completion behavior depends on the shell and its configuration, but the habit is useful everywhere: let the shell finish long names before you press Enter.

Advanced cd Command Techniques

After the basic movement patterns feel natural, cd can also help with symbolic links, repeat destinations, and temporary directory changes.

Follow or Resolve Symbolic Links with cd -L and cd -P

Bash uses logical paths by default, so a symlink can remain visible in pwd even when the real directory lives somewhere else. Create a symlink inside the demo tree:

cd ~/cd-demo
ln -s logs linked-logs

Use the default logical behavior and compare it with the physical path:

cd -L ~/cd-demo/linked-logs
pwd
pwd -P

Example output:

/home/user/cd-demo/linked-logs
/home/user/cd-demo/logs

Use -P when scripts or backups need the physical filesystem path instead of the symlink path:

cd ~/cd-demo
cd -P linked-logs
pwd -P

Example output:

/home/user/cd-demo/logs

Physical paths are often clearer when checking directory sizes with the du command or writing scripts that must avoid symlink ambiguity.

Use CDPATH to Create cd Directory Shortcuts

The CDPATH variable defines base directories that cd searches when you type a relative directory name. This lets you jump to a frequently used project from another location:

cd ~
CDPATH=".:$HOME/cd-demo/projects:$HOME/Documents"
cd webapp
pwd

Expected Bash output:

/home/user/cd-demo/projects/webapp
/home/user/cd-demo/projects/webapp

The first line comes from cd because Bash prints the matched directory when CDPATH finds it outside the current directory. The second line comes from pwd. Add the CDPATH=... line to ~/.bashrc or ~/.zshrc only after confirming the search order matches how you work.

Include ., the current directory, at the start of CDPATH. Without it, cd may jump to a matching name elsewhere instead of a subdirectory beside you.

Combine cd with Other Commands

Pair cd with setup or verification commands when a directory change is only one step in the workflow. Create a directory and enter it in one line:

mkdir -p ~/cd-demo/newapp && cd ~/cd-demo/newapp
pwd

Example output:

/home/user/cd-demo/newapp

The next example moves only files in the disposable demo directory. Check the destination before using mv with real files because matching filenames can be overwritten.

Move log files into the demo archive directory, then switch to that directory to verify the result:

cd ~/cd-demo/logs
mv *.log archive/
cd archive
ls -1

Example output:

access.log
error.log
system.log

For workflows where you need to enter a directory and then return automatically, Bash and Zsh provide pushd and popd. These built-ins maintain a directory stack, which is useful when you move files with the mv command or run several commands in a temporary location:

cd ~/cd-demo
pushd logs
popd

Example Bash output:

~/cd-demo/logs ~/cd-demo
~/cd-demo

Troubleshoot Common cd Command Errors

The cd command usually fails for a small set of reasons: the path does not exist, the path is not a directory, the shell split an unquoted name, or your account lacks execute permission on the directory.

Fix “No such file or directory” Errors

This error appears when the destination path does not exist or the shell expands it to the wrong location:

cd ~/cd-demo/missing

Example Bash error:

bash: cd: /home/user/cd-demo/missing: No such file or directory

List the parent directory to find the correct name:

ls -1 ~/cd-demo

Example output:

Documents
logs
My Documents
projects

Use one of the listed directory names, or create the missing path before trying to enter it.

Fix “Permission denied” Errors

This error means the directory exists, but your account lacks the execute permission needed to enter it:

cd /root

Expected Bash error for a normal user:

bash: cd: /root: Permission denied

Check the directory permissions:

ls -ld /root

Example output:

drwx------ 4 root root 4096 Jan 15 10:30 /root

The drwx------ mode means only the root owner can enter that directory. If you truly need to work there, start an administrative shell first, then change directories inside that shell:

sudo -i
cd /root
pwd

Expected output inside the root shell:

/root

Type exit when you are done to return to your normal user shell. For directories you own, fix the directory permissions instead of opening a root shell; the guide to fix directory permissions with chmod explains the execute bit that controls directory traversal.

Fix “Not a directory” Errors

This error appears when the path exists but points to a file instead of a directory:

touch ~/cd-demo/config.txt
cd ~/cd-demo/config.txt

Example Bash error:

bash: cd: /home/user/cd-demo/config.txt: Not a directory

Use file to confirm what the path points to:

file ~/cd-demo/config.txt

Example output:

/home/user/cd-demo/config.txt: empty

Open files with an editor or viewer. Use cd only for directories.

Fix “Too many arguments” with Spaces in Paths

Bash splits unquoted spaces before cd sees the path. A directory name such as My Documents must be quoted or escaped:

cd ~/cd-demo
cd My Documents

Expected Bash error:

bash: cd: too many arguments

Quote the full directory name to fix it:

cd "My Documents"
pwd

Example output:

/home/user/cd-demo/My Documents

Fix “OLDPWD not set” with cd –

The cd - shortcut needs a previous directory in OLDPWD. Use a temporary subshell to reproduce the unset state without changing your current shell:

(unset OLDPWD; cd -)

Expected Bash error:

bash: cd: OLDPWD not set

Change to another directory first, then use cd - to return:

cd ~/cd-demo/logs
cd ~/cd-demo
cd -

Expected output:

/home/user/cd-demo/logs

cd Does Not Affect the Parent Shell

Running cd inside a script changes only that script’s process. The terminal that launched the script stays where it was:

cat > ~/cd-demo/go-to-logs.sh <<'EOF'
#!/usr/bin/env bash
cd "$HOME/cd-demo/logs"
EOF
bash ~/cd-demo/go-to-logs.sh
pwd

Example output when you started from ~/cd-demo:

/home/user/cd-demo

Source the script only when you trust its contents and want it to affect your current shell:

source ~/cd-demo/go-to-logs.sh
pwd

Expected output:

/home/user/cd-demo/logs

The source command, also available as ., runs the file in the current shell session. That is useful for trusted shell setup files but risky for scripts you have not inspected.

Clean Up the cd Demo Directory

Remove the practice tree when you no longer need the examples:

The cleanup command permanently removes ~/cd-demo and everything inside it. Run it only if you used the demo path from this article and did not store your own files there.

rm -rf ~/cd-demo

Verify the directory is gone:

test ! -e ~/cd-demo && echo "cd demo removed"
cd demo removed

Conclusion

Directory navigation becomes easier when cd, pwd, quoting, and tab completion work together. Use cd - for quick toggles, .. for parent paths, and CDPATH only when the shortcut search order is predictable. For nearby filesystem tasks, pair this workflow with guides to create directories using mkdir or move files safely with mv.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show our tutorials more often in Top Stories and mark them as preferred in AI Mode and AI Overviews when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<a href="https://example.com">link</a> link
<blockquote>quote</blockquote> quote block

Add to the discussion

Questions, fixes, command output, and version notes help keep this guide current.

Verify before posting: