Introduction

Hello! In this article, we’ll learn about the cd command.

cd stands for “Change Directory”, and it’s a basic command used to change your current working directory. It’s one of the most frequently used commands when working with Linux.

What is the cd Command?

The cd command is a shell built-in command used to move (change) your working directory. By being able to move freely within the file system, you can access files and directories in various locations.

Basic Syntax

1
cd [directory path]

Main Usage Patterns

Moving with Absolute Path

Specify the full path starting from the root directory (/).

1
cd /home/user/documents

Moving with Relative Path

Specify a location relative to your current directory.

1
2
cd documents
cd ../parent-directory

Useful Special Symbols

Symbol Meaning
~ Home directory
. Current directory
.. Parent directory (one level up)
- Previous directory

Usage Examples

Example 1: Go to Home Directory

1
2
3
cd ~
# or simply
cd

Result:

1
# Moves to your home directory (like /home/username)

Example 2: Go to Parent Directory

1
cd ..

Result:

1
# Moves up one directory level

Example 3: Return to Previous Directory

1
2
3
cd /home/user/documents
cd /etc
cd -

Result:

1
2
/home/user/documents
# Returns to /home/user/documents

Example 4: Go Up Multiple Levels

1
cd ../../..

Result:

1
# Moves up three directory levels

Example 5: Move with Absolute Path

1
cd /var/log

Result:

1
# Moves to /var/log directory

Tips & Notes

  • cd without arguments: Running cd without arguments takes you to your home directory
  • Paths with spaces: If a path contains spaces, enclose it in quotes (") or escape with backslash (\)
    1
    2
    
    cd "My Documents"
    cd My\ Documents
    
  • Tab completion: When typing paths, press Tab for auto-completion
  • Check current location: Use the pwd command to see your current directory
    1
    
    pwd
    
  • Non-existent directory: Trying to move to a non-existent directory will give an error
    1
    2
    
    cd nonexistent-directory
    # bash: cd: nonexistent-directory: No such file or directory
    

Practical Usage

Relative Path from Home Directory

You can use ~ to specify a relative path from your home directory.

1
cd ~/projects/myapp

CDPATH Environment Variable

If you frequently access certain directories, setting the CDPATH environment variable can be convenient.

1
2
export CDPATH=.:~:~/projects
cd myapp  # Can move to ~/projects/myapp

Summary

In this article, we learned about the cd command.

Key Points:

  • cd is a basic command for moving between directories
  • Can use both absolute and relative paths
  • Special symbols like ~, ., .., - make navigation efficient
  • Tab completion makes typing easier

The cd command is absolutely fundamental to Linux operations. Master it well for a comfortable command-line experience!

Keep learning Linux commands!