The cd command in Linux changes your current working directory in the terminal. Whether you need to navigate into project folders, jump between log directories, or quickly return home, cd is the command you will use most often. This guide covers every cd shortcut and pattern, from basic directory changes to symbolic link handling, CDPATH shortcuts, and common error fixes.
Understand the cd Command in Linux
Unlike most Linux commands, cd is a shell built-in rather than a standalone program. It runs inside your current shell session, which means it can change the shell’s working directory directly. External programs cannot do this, which is why cd must be built into every shell (Bash, Zsh, Fish, and others).
cd Command Syntax and Options
The general syntax of the cd command is:
cd [options] [directory]
Both arguments are optional. Running cd alone returns you to your home directory. The [directory] argument accepts absolute paths (starting from /) or relative paths (from your current location). Two options control how cd handles symbolic links:
-L(default) follows symbolic links, sopwdshows the logical path through the symlink.-Presolves symbolic links to their physical location, sopwdshows the actual directory on disk.
The cd command also relies on two environment variables. OLDPWD stores the previous working directory (used by cd -), and CDPATH defines a search path for directory names (covered in the advanced section below).
Verify cd Is Available
Since cd ships as a shell built-in on every Linux distribution, it does not require installation. Confirm it with type:
type cd
cd is a shell builtin
Quick Reference: cd Command Shortcuts
The table below summarizes the most common cd patterns. Each shortcut is explained in detail in the sections that follow.
| Task | Command | What It Does |
|---|---|---|
| Home directory | cd or cd ~ | Returns to your home directory |
| Previous directory | cd - | Switches to the last visited directory |
| Parent directory | cd .. | Moves up one level |
| Up two levels | cd ../.. | Moves up two directory levels |
| Root directory | cd / | Navigates to the filesystem root |
| Absolute path | cd /var/log | Navigates to an exact filesystem path |
| Relative path | cd Documents | Navigates relative to your current location |
| Spaces in name | cd "My Folder" | Handles directory names containing spaces |
| Another user’s home | cd ~username | Navigates to another user’s home directory |
The examples below cover every common navigation pattern. Each includes the command, expected output, and a brief explanation of when the pattern is most useful.
Change to a Specific Directory
Pass a directory name to cd to move into it. The directory must exist inside your current working directory:
cd Documents && pwd
/home/user/Documents
The pwd (print working directory) command confirms your new location after the change.
Change Directory and List Contents
Chain cd with ls using && to move into a directory and immediately view its contents. The && operator ensures ls only runs if cd succeeds:
cd /var/log && ls
alternatives.log auth.log dpkg.log kern.log syslog wtmp
Navigate Using an Absolute Path
An absolute path starts from the root directory (/) and specifies the full location. Use absolute paths when you know the exact destination regardless of your current directory:
cd /etc/nginx && pwd
/etc/nginx
Navigate Using a Relative Path
A relative path starts from your current directory rather than the root. This is faster when the target is nearby in the directory tree:
cd projects/webapp && pwd
/home/user/projects/webapp
The shell resolves projects/webapp relative to your current working directory, so the result depends on where you start.
Return to the Previous Directory with cd –
The cd - shortcut switches back to the directory you were in before the last cd call. It reads the OLDPWD environment variable and prints the path it switches to:
cd /var/log
cd /etc
cd -
/var/log
Running cd - repeatedly toggles between two directories, which is useful when editing a configuration file in one location and checking logs in another.
Move to the Parent Directory with cd ..
The .. entry in every directory refers to its parent. Use it to move up one level:
cd /var/log/nginx
cd .. && pwd
/var/log
Move Up Multiple Directory Levels
Chain .. entries with slashes to move up several levels at once:
cd /var/log/nginx
cd ../.. && pwd
/var
Each .. moves up one level. The pattern ../../.. would move up three levels, and so on.
Navigate to the Root Directory
The root directory (/) is the top-level entry in the Linux filesystem hierarchy. Every absolute path starts from here:
cd / && pwd
/
Return to the Home Directory
Running cd with no arguments or with ~ both return you to your home directory. Both forms read the HOME environment variable:
cd
pwd
/home/user
The ~ shorthand is more explicit and also works as part of longer paths like cd ~/Documents:
cd ~/Documents && pwd
/home/user/Documents
Access Another User’s Home Directory
The tilde followed by a username expands to that user’s home directory. You need read and execute permissions on the target directory:
cd ~www-data && pwd
/var/www
Replace www-data with the target username. If the directory has restrictive permissions, you may need to prefix the command with sudo.
Handle Directory Names with Spaces
Wrap directory names that contain spaces in double quotes, or escape each space with a backslash. Without either method, the shell interprets each word as a separate argument:
cd "My Documents" && pwd
/home/user/My Documents
The backslash escape method achieves the same result:
cd My\ Documents && pwd
/home/user/My Documents
Navigate Nested Subdirectories
Provide a path with multiple directory levels separated by slashes to jump directly into a nested subdirectory:
cd /var/log/apt && pwd
/var/log/apt
This works with both 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 if there is a single match; if multiple directories share the same prefix, press Tab twice to see all options:
cd /etc/n<Tab><Tab>
netplan/ NetworkManager/ nginx/
Tab completion reduces typos and speeds up navigation, especially inside directories with long or complex names.
Advanced cd Command Techniques
Beyond basic navigation, cd supports options and environment variables that simplify repetitive workflows.
Follow or Bypass Symbolic Links with cd -P and cd -L
By default, cd follows symbolic links (the -L behavior). The -P flag resolves the symlink to its physical target instead. The difference is visible in the output of pwd:
ln -s /var/log /tmp/logs
cd -L /tmp/logs && pwd
/tmp/logs
cd -P /tmp/logs && pwd
/var/log
Use -P in scripts where you need the real filesystem path, for example when checking directory sizes with the du command or performing backups.
Use CDPATH to Create cd Directory Shortcuts
The CDPATH variable defines a list of base directories that cd searches when you provide a relative name. This lets you jump to frequently used directories from anywhere without typing the full path:
export CDPATH=".:$HOME/projects:$HOME/Documents"
cd webapp && pwd
/home/user/projects/webapp
The shell searched the current directory (.), then ~/projects, found webapp there, and changed into it. Add the export CDPATH line to your ~/.bashrc or ~/.zshrc to make it permanent.
Always include
.(the current directory) at the start ofCDPATH. Without it,cdmay skip a subdirectory in your current location in favor of a match in anotherCDPATHdirectory.
Combine cd with Other Commands
Several common workflows pair cd with other commands for efficient directory management:
Create a directory and move into it in a single line:
mkdir -p ~/projects/newapp && cd ~/projects/newapp
Move files to a directory, then switch to it to verify the result:
mv *.log /var/log/archive/ && cd /var/log/archive/ && ls
For workflows where you need to temporarily enter a directory and return automatically, Bash provides pushd and popd. These maintain a directory stack so you can move files with the mv command into a target, process them, and return:
pushd /etc/nginx
# edit configuration files
popd
/etc/nginx ~ ~
Troubleshoot Common cd Command Errors
The cd command produces clear error messages when something goes wrong. Below are the most common issues and how to resolve them.
“No such file or directory” Error
This error appears when the path you specified does not exist:
cd /var/logs
bash: cd: /var/logs: No such file or directory
The cause is usually a typo (in this case, /var/logs instead of /var/log) or a directory that was deleted or renamed. List the parent directory to find the correct name:
ls /var/
backups cache lib local lock log mail opt run spool tmp
The correct directory name is /var/log, not /var/logs.
“Permission denied” Error
This error means the directory exists, but your user account lacks the execute permission needed to enter it:
cd /root
bash: cd: /root: Permission denied
Check the directory permissions with ls -ld:
ls -ld /root
drwx------ 4 root root 4096 Jan 15 10:30 /root
The drwx------ permissions show only the root owner can access this directory. Use sudo to enter it, or ask an administrator to adjust the directory permissions with chmod.
“Not a directory” Error
This error occurs when you attempt to cd into a file rather than a directory:
cd /etc/hostname
bash: cd: /etc/hostname: Not a directory
Verify whether the path is a file or directory with the file command:
file /etc/hostname
/etc/hostname: ASCII text
cd Does Not Affect the Parent Shell (Subshell Issue)
Running cd inside a script has no effect on your current terminal session. Each script runs in a subshell, and the cd only changes the directory within that subshell:
cat go-to-logs.sh
#!/bin/bash cd /var/log
bash go-to-logs.sh
pwd
/home/user
The working directory did not change because the script ran in a child process. To run the cd in your current shell, source the script instead:
source go-to-logs.sh
pwd
/var/log
The source command (or its shorthand .) executes the script in the current shell session, so the cd takes effect.
Frequently Asked Questions about the cd Command
cd changes your working directory and stores the previous location in OLDPWD. pushd also changes your directory but adds the previous location to a stack, letting you return with popd. Use pushd/popd when you need to visit several directories and return to each in reverse order.
The core cd behavior (changing directories, cd -, cd .., tilde expansion) is identical across Bash, Zsh, and Fish because it is a POSIX standard. Minor differences exist in tab completion features and CDPATH handling, but the navigation commands shown in this guide work in all three shells.
Run the pwd (print working directory) command. It outputs the full absolute path of your current location. Most shell prompts also display the current directory or an abbreviated version of it.
Conclusion
The cd command handles all directory navigation in Linux, from basic moves with cd .. and cd ~ to toggling between locations with cd - and setting up CDPATH shortcuts. Pair these patterns with tab completion, and you can reach any directory on the filesystem in a few keystrokes.
Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>