Change Directory

Change the current working directory to a specified path.

filesystem
navigation
basic
linux

Command

cd

Explanation

The `cd` command allows navigation between directories. When you specify an absolute path (starting with `/`), it moves directly to that location. Relative paths (like `../folder`) move based on your current directory. Using `cd` without arguments takes you to your home directory.

Common Use Cases

  • Navigate to project directories
  • Move up or down directory levels
  • Return quickly to home or root directory

Best Practices

  • Use absolute paths for scripts to avoid path confusion
  • Combine with `pwd` to verify your location after changing directories
  • Use tab completion to avoid typing long paths

Common Mistakes to Avoid

  • Using backslashes instead of forward slashes in paths
  • Forgetting quotes around paths with spaces
  • Assuming `cd` works in non-interactive scripts without environment setup

Troubleshooting

Problem: No such file or directory error

Solution: Verify that the path exists using `ls` before running `cd`.

Problem: Permission denied

Solution: Ensure you have access rights or use `sudo` cautiously.

Examples

Move to an absolute directory path

cd /var/www

Move up one directory level

cd ..

Go to the user's home directory

cd ~