cd
Basic Usage of cd - Changing Directories
The cd command is used to change the current working directory in Linux. It allows users and administrators to navigate through the filesystem.
cd directory_name
Example:
cd Documents
cdstands for “change directory”- Changes current working location
- One of the most frequently used Linux commands
- Essential for filesystem navigation
Checking Current Directory
Before changing directories, administrators often verify their current location.
pwd
Example output:
/home/user
pwdstands for “print working directory”- Displays current filesystem location
- Commonly used together with
cd
Moving Into a Directory
To enter a subdirectory:
cd Downloads
If current location is:
/home/user
After running the command:
/home/user/Downloads
- Relative paths are based on current directory
- Most common navigation method
Moving Back to Parent Directory
To move one level upward:
cd ..
Example:
Current directory:
/home/user/Documents
After command:
/home/user
..means parent directory- Useful for navigating upward in filesystem hierarchy
Moving to Home Directory
To return directly to home directory:
cd
or
cd ~
Example result:
/home/user
~represents current user’s home directory- Common shortcut in Linux shells
- Frequently used during administration tasks
Moving to Previous Directory
Linux remembers the previous directory location.
cd -
Example:
/home/user/Documents
Move to another directory:
cd /var/log
Return instantly:
cd -
Output:
/home/user/Documents
-means previous directory- Extremely useful during troubleshooting
- Saves time switching between locations
Using Absolute Paths
Absolute paths begin from filesystem root (/).
Example:
cd /var/log
- Starts from root directory
- Works regardless of current location
- Common in scripts and automation
Another example:
cd /etc/nginx
Using Relative Paths
Relative paths begin from current directory.
Example:
cd projects/backend
If current location is:
/home/user
Result:
/home/user/projects/backend
- Simpler for nearby directories
- Common during development work
Understanding Special Path Symbols
Linux navigation uses special symbols:
| Symbol | Meaning |
|---|---|
. |
Current directory |
.. |
Parent directory |
~ |
Home directory |
/ |
Root filesystem |
Example:
cd ./scripts
.explicitly means current directory
Moving Multiple Levels Up
To move two levels upward:
cd ../..
Example:
Current location:
/home/user/projects/backend
After command:
/home/user
- Each
..moves up one directory level - Useful in deep directory structures
Navigating with Spaces in Directory Names
Directories containing spaces require quotes or escaping.
Example using quotes:
cd "My Documents"
Example using escape characters:
cd My\ Documents
- Spaces separate command arguments in shell
- Quotes preserve full directory name
- Backslash escapes special characters
Tab Completion
Most Linux shells support tab completion.
Example:
cd Doc<TAB>
Automatically expands to:
cd Documents
- Saves typing
- Reduces mistakes
- Very important for productivity
Common Administrative Navigation Examples
Move to log directory:
cd /var/log
Move to web server configuration:
cd /etc/nginx
Move to Apache configuration:
cd /etc/apache2
Move to user SSH configuration:
cd ~/.ssh
Combining cd with ls
Administrators often combine navigation with listing files.
Example:
cd /var/log && ls -lh
Breakdown:
cd /var/logchanges directory&&runs second command only if first succeedsls -lhlists directory contents
This is commonly used during troubleshooting.
Practical Script Example (Step-by-Step Explanation)
Script
#!/bin/bash
TARGET="/var/log"
cd $TARGET || exit
echo "Current directory:"
pwd
ls -lh
Step 1: Shebang
#!/bin/bash
- Defines Bash interpreter
- Ensures script runs using Bash shell
Step 2: Variable definition
TARGET="/var/log"
- Stores target directory in variable
- Makes script easier to maintain
Example value:
/var/log
Step 3: Changing directory
cd $TARGET || exit
Breakdown:
cd $TARGETattempts directory change$TARGETexpands variable value|| exitmeans exit script if command fails
This prevents errors later in script execution.
Example:
If /var/log does not exist:
- script immediately stops
- avoids running commands in wrong location
Step 4: Displaying current directory
pwd
- Prints current working directory
- Confirms successful navigation
Example output:
/var/log
Step 5: Listing directory contents
ls -lh
- Lists files in long format
- Displays human-readable file sizes
- Useful for quick inspection
What this script does
Step-by-step flow:
- Defines target directory
- Changes into target location
- Stops execution if directory change fails
- Displays current directory
- Lists files in readable format
Why this matters in production
This pattern is extremely common in:
- deployment scripts
- backup automation
- server maintenance
- log analysis
- DevOps workflows
The || exit pattern is especially important because it prevents dangerous mistakes caused by running commands in incorrect directories.
Common Beginner Mistakes
Trying to execute cd without existing directory:
cd missing_directory
Result:
bash: cd: missing_directory: No such file or directory
Another common mistake:
Using spaces without quotes:
cd My Documents
Correct version:
cd "My Documents"
Summary
In this guide, you learned:
- how to navigate directories using
cd - difference between absolute and relative paths
- how to return to home directory
- how to switch to previous directory
- how Linux special path symbols work
- how to handle spaces in directory names
- how to combine
cdwith other commands - how directory navigation works in scripts
These skills are essential for:
- Linux administration
- shell scripting
- filesystem navigation
- troubleshooting
- DevOps workflows
Additional cd-related concepts not covered in this guide include:
CDPATH environment variable
symbolic link navigation
pushd and popd commands
shell directory stack
autojump and zoxide tools
shell aliases for navigation