cd — Linux basic command
Linux / Basic Linux commands / cd.html
A command to change the current working directory to another directory.
Basic Information
| Command Name | cd (change directory) |
|---|---|
| One-line Introduction | A command to change the current working directory to another directory. |
| Provided | Bash/Zsh shell built-in command (included by default in all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/cd.1p.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL (shell built-in) |
| Installation | Included by default (no separate installation required) |
| Classification | Linux basic command (tags: directory navigation, shell, Linux, basic) |
What is this command?
cd is short for change directory, a shell built-in command that moves the current working directory to another location. It is a core tool for Linux file navigation.
It supports both relative and absolute paths, and you can use special symbols like ~ (home), - (previous directory), and .. (parent).
What problems does it solve?
- Navigate to and work in other directories.
- Quickly return to the previous directory.
- Move to the home directory.
- Move to the parent directory.
Key Features
Absolute Path Navigation
Move to a full path starting with /.
- Usage Scenario: When you know the exact location.
- Result Obtained: Move to the same path from anywhere.
- Limitations: You must know the exact path.
Relative Path Navigation
Use paths relative to the current location.
- Usage Scenario: When exploring around the current directory.
- Result Obtained: Move quickly with shorter paths.
- Limitations: You must know the current location for correct operation.
Special Symbols (~, -, ..)
~ indicates home, - indicates previous directory, and .. indicates parent directory.
- Usage Scenario: When quickly moving to frequently visited locations.
- Result Obtained: Reduce typing and move quickly.
- Limitations: - may behave differently depending on the shell.
PROMPT Directory Display
After navigation, the current directory is displayed in the shell prompt.
- Usage Scenario: When working while continuously checking the current location.
- Result Obtained: Never lose track of your working location.
- Limitations: The display method varies based on prompt settings.
Installation and Execution
Verify Default Installation
It is built into all shells.
# cd is a shell built-in command
type cd
# Result: cd is a shell builtin
Set Directory Display in Shell Prompt
Configure the prompt to display the current directory.
# Add to ~/.bashrc
export PS1='\u@\h:\w\$ '
# Apply changes
source ~/.bashrc
Practical Usage Examples
Basic Navigation
Navigate directories using absolute and relative paths.
# Absolute path navigation
cd /var/log
# Relative path navigation
cd ../documents
# Move to home directory
cd ~
# or
cd
Using Special Symbols
Quickly move using ~, -, .. symbols.
# Home directory
cd ~
# Return to previous directory
cd -
# Parent directory
cd ..
# Current directory (do nothing)
cd .
Abbreviated Directory Names
Navigate using only the unique leading characters recognized by the shell.
# To move to /var/log
# Assuming there are logs and log under /var
cd /var/lo # Auto-complete to select /var/log
# Auto-complete with Tab key
cd /var/TAB