How to Change Directory in Linux: Simple cd Command Guide
Use the
cd command followed by the directory path to change directories in Linux. For example, cd /home/user moves you to the /home/user directory.Syntax
The basic syntax of the cd command is:
cd [directory_path]: Changes the current directory to the specifieddirectory_path.- If no path is given,
cdmoves to the user's home directory. - Special shortcuts include
cd ..to move up one directory andcd -to return to the previous directory.
bash
cd [directory_path]
Example
This example shows how to change to the /var/log directory and then back to the home directory.
bash
pwd cd /var/log pwd cd pwd
Output
/home/user
/var/log
/home/user
Common Pitfalls
Common mistakes when using cd include:
- Typing a directory path that does not exist, which causes an error.
- Forgetting that paths are case-sensitive in Linux.
- Not using quotes when the directory name contains spaces.
Example of wrong and right usage:
bash
cd /NonExistentDir # Output: bash: cd: /NonExistentDir: No such file or directory cd "My Documents" # Correct if directory has spaces
Output
bash: cd: /NonExistentDir: No such file or directory
Quick Reference
| Command | Description |
|---|---|
| cd | Go to home directory |
| cd /path/to/dir | Go to specified directory |
| cd .. | Go up one directory level |
| cd - | Go to previous directory |
| cd ~ | Go to home directory |
Key Takeaways
Use
cd [directory] to change directories in Linux.Paths are case-sensitive and must exist to avoid errors.
Use
cd .. to move up one directory level.Use quotes around directory names with spaces.
Typing
cd alone returns you to your home directory.