Skip to main content

cd — Change Directory

The cd command is the foundation of navigation in Linux and Unix systems.
Every file operation, configuration edit, backup, or deployment begins with being in the correct directory.

If pwd tells you where you are, cd determines where you go next.

In VPS and WordPress environments, mastering cd prevents mistakes such as:

  • Editing the wrong configuration file
  • Deleting incorrect directories
  • Running scripts in unintended locations
  • Breaking deployments due to path confusion

Understanding cd is essential for safe and efficient system management.

What cd Does

cd changes the current working directory of your shell session.

After running cd, every relative command operates from the new location.

Example:

cd /var/www/html

Now all commands (like ls, rm, nano, cp) operate inside /var/www/html.

How Directory Navigation Works

Linux uses a hierarchical filesystem structure:

/
├── home
├── var
│ └── www
├── etc
├── tmp

Navigation types:

  • Absolute path → Starts from root /
  • Relative path → Starts from your current directory
  • Special shortcuts~, -, ., ..

Core Syntax

cd [DIRECTORY]

If no directory is specified:

cd

You return to your home directory.

Common Variations

CommandMeaningExample Result
---
cdGo to home directory/home/user
cd ~Home directory shortcut/home/user
cd -Switch to previous directory/etc/nginx
cd ..Go one level up/var/www
cd .Stay in current directoryNo change
cd /path/to/dirAbsolute navigation/var/www/html
cd relative/pathRelative navigationwp-content/plugins

Absolute vs Relative Paths

Absolute Path

Starts from / (root). Safe for scripts.

cd /var/www/html

Relative Path

Depends on your current directory.

If you're inside /var/www:

cd html/wp-content

Results in:

/var/www/html/wp-content

Why cd Matters in VPS & WordPress

Typical WordPress paths:

  • /var/www/html
  • /var/www/html/wp-content/plugins
  • /var/www/html/wp-content/themes
  • /var/www/html/wp-content/uploads
  • /var/log/nginx
  • /etc/php/8.3/fpm

Before modifying files or restarting services, navigate correctly using cd.

Best Practices

  • Always run pwd after cd to confirm location.
  • Prefer absolute paths in scripts.
  • Use cd - to toggle between two directories quickly.
  • Avoid destructive commands (rm -rf, mv) without verifying location.
  • Use TAB auto-completion to avoid typos.

Practical Lab — 25 Real-World Examples

1. Go to home directory

cd
pwd

2. Home shortcut

cd ~
pwd

3. WordPress root

cd /var/www/html
pwd

4. Plugins directory

cd /var/www/html/wp-content/plugins
pwd

5. Themes directory

cd /var/www/html/wp-content/themes
pwd

6. Uploads directory

cd /var/www/html/wp-content/uploads
pwd

7. Move one level up

cd ..
pwd

8. Move two levels up

cd ../..
pwd

9. Stay in current directory

cd .
pwd

10. Toggle previous directory

cd -
pwd

11. Apache logs

cd /var/log/apache2
pwd

12. Nginx logs

cd /var/log/nginx
pwd

13. PHP configuration

cd /etc/php/8.3/fpm
pwd

14. MariaDB configuration

cd /etc/mysql
pwd

15. Backup directory

cd /backups
pwd

16. SSH configuration

cd ~/.ssh
pwd

17. Temporary directory

cd /tmp
pwd

18. Root directory

cd /
pwd

19. Relative navigation

If inside /var/www:

cd html/wp-content
pwd

20. TAB auto-completion

cd /var/www/html/wp-co[TAB]

Auto-completes to wp-content.

21. Wrong path example

cd /var/ww/html

Output:

No such file or directory

22. Edit WordPress config

cd /var/www/html
nano wp-config.php

23. Create and enter new directory

mkdir /var/www/test && cd /var/www/test
pwd

24. Quick navigation + confirmation

cd /var/log && pwd

25. Combine with script

echo "Current directory: $(pwd)"

Troubleshooting Matrix

ProblemCauseSolution
---
No such fileTypo in pathUse TAB completion
Wrong directoryForgot confirmationRun pwd
Script failureRelative path usedSwitch to absolute paths
Permission deniedRestricted folderUse sudo if appropriate

Cheat Sheet

cd # Home directory
cd ~ # Home directory
cd - # Previous directory
cd .. # One level up
cd . # Current directory
cd /path # Absolute path
cd relative # Relative path

Final Perspective

cd is not just navigation. It defines execution context.

Before modifying:

  • WordPress plugins
  • Themes
  • Logs
  • Config files
  • Backups

Always ensure you are in the correct directory.

Navigation errors cause most accidental system damage — and cd is your primary control mechanism.

Mini Knowledge Check

  1. What is the difference between cd - and cd ..?
  2. Why should scripts prefer absolute paths?
  3. What does cd do with no arguments?
  4. If you are in /var/www/html, what does cd ../.. return?

If you'd like next:

- A combined navigation module (`pwd`, `cd`, `ls`, `tree`)
- A WordPress VPS directory structure guide with diagrams
- Or a filesystem fundamentals deep dive

Just tell me the direction.