Overview
The cd (change directory) command is a shell built-in tool used to change your current working directory. The Linux file system is organized in a tree structure. To perform file operations or execute commands, you must be able to move to the correct directory. This article explains basic navigation, the difference between physical and logical paths when handling symbolic links, and techniques for quickly returning to your previous directory to improve workflow.
Specifications (Arguments and Options)
Syntax
cd [options] [directory_name]
Main Options
While cd is usually used without options, you can use the following to control how symbolic links are handled. | Option | Description | | :— | :— | | -L | Follows symbolic links and moves to the logical path (default). | | -P | Resolves symbolic links and moves to the physical path (Physical). | | - | Moves to the previous working directory. |
Basic Usage
Moving to Specific Directories and Returning Home
Provide a path as an argument to move to that location. If you run the command without any arguments, it returns you to the current user’s home directory.
# Move using an absolute path
cd /usr/share/doc
# Check your current location
pwd
# Return to the home directory (no arguments)
cd
# Check your current location again
pwd
Example Output:
/usr/share/doc
/home/user
Practical Commands
1. Moving to the Physical Directory of a Symbolic Link (-P)
Normally, when you move to a directory that is a symbolic link, the path remains shown as the link name (logical path). Using the -P option moves you to the actual underlying directory (physical path). In this example, assume current_project is a link pointing to /opt/project_v2.
# Check the status of the link
ls -l current_project
# Move to the physical path (-P)
cd -P current_project
# Display the current directory (the actual path is shown)
pwd
Example Output:
lrwxrwxrwx 1 user group 16 Jan 15 10:00 current_project -> /opt/project_v2
/opt/project_v2
2. Returning to the Previous Directory (-)
When working between two directories, typing the paths repeatedly is inefficient. Using cd - allows you to jump back to the previous directory instantly.
# 1. Move to a temporary directory
cd /var/tmp
echo "Current location: $(pwd)"
# 2. Return to the previous directory
cd -
Example Output:
Current location: /var/tmp
/home/user
Note: When you run cd -, the target path is displayed in the standard output.
3. Checking the Behavior of the OLDPWD Environment Variable
The cd - command works because the shell stores the previous path in the OLDPWD environment variable. If you delete this variable, you cannot return using that shortcut.
# Check the current OLDPWD
echo $OLDPWD
# Delete the variable
unset OLDPWD
# Attempting to return will result in an error
cd -
Example Output:
/var/tmp
bash: cd: OLDPWD not set
Customization Points
- Use Tab Completion: Instead of typing full directory names, use the Tab key to complete paths automatically. This prevents typos.
- Home Directory Shortcuts: Besides running
cdalone, you can use the tilde~to specify paths under your home directory, such ascd ~/Documents. - Moving Upward: Use
cd ..to move up one level. You can usecd ../..to move up two levels.
Important Notes
- Permission Errors: You cannot move into a directory if you do not have execute (
x) permissions for it. - Navigation Failures in Scripts: When using
cdin shell scripts, it is dangerous if subsequent commands run despite a navigation failure (e.g., an unintendedrmcommand). Always use logical AND (&&) likecd /target/dir && command, or implement proper error handling. - Directories with Spaces: If a directory name contains spaces, like
Program Files, you must wrap it in quotes (cd "Program Files") or escape the space (cd Program\ Files).
Advanced Application
Using the Directory Stack (pushd / popd)
While cd - only remembers one previous location, pushd and popd allow you to store multiple directories in a “stack” and return to them in order.
# Move while remembering the history
pushd /var/log
pushd /etc/nginx
# Return to previous locations in reverse order
popd
popd
Conclusion
The cd command is the starting point for almost all operations in Linux.
Caution: Be careful about navigation failures when writing scripts. Mastering cd - can significantly speed up your work when you need to move frequently between a log directory and a configuration directory.
Ideal for: All CLI tasks.
Key Feature: Use -P when you need to access the actual physical location of a link.
