0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use cd Command in Linux: Syntax and Examples

The cd command in Linux changes the current directory to another directory you specify. Use cd <directory_path> to move to that directory, or cd .. to go up one level.
📐

Syntax

The basic syntax of the cd command is:

  • cd <directory_path>: Changes to the specified directory.
  • cd ..: Moves up one directory level (to the parent directory).
  • cd ~ or just cd: Moves to your home directory.
  • cd -: Switches to the previous directory you were in.
bash
cd [directory_path]
💻

Example

This example shows how to change directories using cd. It moves to the /usr directory, then up one level, then back to the home directory.

bash
pwd
cd /usr
pwd
cd ..
pwd
cd ~
pwd
Output
/home/user /usr / /home/user
⚠️

Common Pitfalls

Common mistakes when using cd include:

  • Typing a directory name that does not exist causes an error.
  • Forgetting that cd is case-sensitive.
  • Using relative paths incorrectly without understanding the current directory.

Always check the directory exists with ls or pwd before changing.

bash
cd /nonexistent
# Output: bash: cd: /nonexistent: No such file or directory

# Correct usage:
cd /usr
Output
bash: cd: /nonexistent: No such file or directory
📊

Quick Reference

CommandDescription
cd /path/to/dirChange to specified directory
cd ..Go up one directory level
cd ~Go to home directory
cd -Switch to previous directory
cdGo to home directory

Key Takeaways

Use cd <directory> to change to a specific directory.
Use cd .. to move up one directory level.
Use cd or cd ~ to return to your home directory.
Directory names are case-sensitive and must exist to avoid errors.
Use cd - to quickly switch back to the last directory.