Cd Command in Linux (Change Directory)

What is cd?

The cd command stands for “change directory”. It allows you to navigate through the filesystem in the Linux terminal.

Syntax

cd [directory_path]
  • directory_path: The target directory you want to switch to.

Step-by-Step Usage

Step 1: Navigate to a Directory

cd Documents

Goes from the current directory to Documents.

 Step 2: Move to Parent Directory (..)

cd ..

Moves up one level in the directory tree.

Example:

  • /home/user/Documents → /home/user

Step 3: Move to Home Directory (~ or just cd)

cd ~

or simply:

cd

Takes you to your home directory (/home/yourusername)

Step 4: Go to Root Directory (/)

cd /

Moves to the root directory (the top of the file system tree)

Step 5: Move to a Full/Absolute Path

cd /etc/apache2

Directly jumps to /etc/apache2, regardless of where you are.

Step 6: Move to a Relative Path

cd projects/myapp

Navigates relative to your current location.

Step 7: Go Back to Previous Directory (-)

cd -

Switches back to the last directory you were in.

Example:

  • From /home/user to /etc, then:

    • cd - → goes back to /home/user

Step 8: Handle Spaces in Directory Names

cd "My Folder"

or

cd My\ Folder

Use quotes or backslash (\) to escape spaces.

Step 9: Tab Autocomplete

While typing:

cd Doc[TAB]

Pressing Tab auto-completes folder names like Documents.

Summary of Useful cd Commands

CommandDescription
cdGo to home directory
cd ..Go up one level
cd /Go to root directory
cd -Switch to the previous directory
cd ~Shortcut to home directory
cd ./folderMove to subfolder in current directory
cd ../..Move up two levels
cd /path/to/dirMove to specific absolute path

Pro Tips

  • Use pwd to print the current directory:

    pwd
  • Combine with ls to see contents before navigating:

    ls && cd folder_name

Example Workflow

cd /var cd log cd .. cd /home/user/Documents cd - cd ~

Let me know if you'd like this in cheat sheet format, with visual diagrams, or as a script to practice!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
Ad