Bird
Raised Fist0
Linux CLIscripting~10 mins

cd (change directory) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - cd (change directory)
Start in current directory
↓
User types 'cd <path>'
↓
Shell checks if <path> exists and is directory
↓
Update current directory
↓
Prompt ready in new directory
The shell changes the current directory if the given path exists; otherwise, it shows an error.
Execution Sample
Linux CLI
pwd
cd /tmp
pwd
cd nonexistent
pwd
Shows current directory, changes to /tmp, shows new directory, tries to change to a non-existent directory, then shows directory again.
Execution Table
StepCommandConditionActionOutput
1pwdN/APrint current directory/home/user
2cd /tmpDoes /tmp exist?Yes, change directory to /tmp
3pwdN/APrint current directory/tmp
4cd nonexistentDoes 'nonexistent' exist?No, show errorbash: cd: nonexistent: No such file or directory
5pwdN/APrint current directory/tmp
💡 Execution stops after last pwd command showing current directory remains /tmp because cd to nonexistent failed.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
current_directory/home/user/tmp/tmp/tmp
Key Moments - 2 Insights
Why does the current directory not change after trying to cd to a non-existent directory?
Because the shell checks if the directory exists before changing. Step 4 in the execution_table shows the condition fails and an error is printed, so the directory stays the same.
What happens if you just type 'cd' without any path?
The shell changes to your home directory. This is a shortcut not shown in the table but is a common behavior.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the current directory after step 3?
Anonexistent
B/home/user
C/tmp
D/
💡 Hint
Check the output column at step 3 in execution_table.
At which step does the shell show an error message?
AStep 2
BStep 4
CStep 5
DStep 1
💡 Hint
Look for the row with an error message in the Output column.
If the directory '/tmp' did not exist, what would happen at step 2?
AShow an error and keep current directory
BChange directory to /tmp anyway
CCreate /tmp directory automatically
DExit the shell
💡 Hint
Refer to the condition and action columns in execution_table for step 2 and 4.
Concept Snapshot
cd command changes the current directory.
Syntax: cd [path]
If path exists and is directory, shell moves there.
If no path, goes to home directory.
If path invalid, shows error and stays put.
Full Transcript
The cd command changes your current folder in the shell. When you type cd followed by a path, the shell checks if that folder exists. If it does, it moves you there. If not, it shows an error and keeps you where you are. For example, starting in /home/user, typing cd /tmp moves you to /tmp. Trying cd nonexistent shows an error and you stay in /tmp. Typing pwd shows your current folder at any time.

Practice

(1/5)
1. What does the command cd .. do in a Linux terminal?
easy
A. Moves you to your home directory
B. Moves you up one directory level
C. Moves you to the root directory
D. Lists files in the current directory

Solution

  1. Step 1: Understand the meaning of .. in directory paths

    The .. symbol means the parent directory, which is one level up from the current directory.
  2. Step 2: Apply cd .. command

    Using cd .. changes the current directory to its parent directory, effectively moving you up one level.
  3. Final Answer:

    Moves you up one directory level -> Option B
  4. Quick Check:

    cd .. = move up one folder [OK]
Hint: Remember: '..' means parent folder, so cd .. goes up [OK]
Common Mistakes:
  • Thinking cd .. moves to home directory
  • Confusing cd .. with cd / (root)
  • Using cd .. to list files
2. Which of the following is the correct syntax to change to your home directory?
easy
A. cd ~
B. cd ..
C. cd /home
D. cd -

Solution

  1. Step 1: Identify the symbol for home directory

    The tilde ~ is a shortcut for the current user's home directory in Linux.
  2. Step 2: Check the command syntax

    cd ~ correctly changes the directory to the home folder. Other options either go up one level, to root, or previous directory.
  3. Final Answer:

    cd ~ -> Option A
  4. Quick Check:

    cd ~ = go home [OK]
Hint: Use ~ to quickly jump to home directory [OK]
Common Mistakes:
  • Using cd .. to go home instead of up one level
  • Using cd /home which may not be correct for all users
  • Using cd - which goes to previous directory
3. If your current directory is /home/user/docs and you run cd ../pictures, what will be your new directory?
medium
A. /home/pictures
B. /home/user/docs/pictures
C. /home/user/pictures
D. /pictures

Solution

  1. Step 1: Understand the starting directory and command

    Current directory is /home/user/docs. The command cd ../pictures means go up one level to /home/user then into pictures.
  2. Step 2: Calculate the new directory path

    Going up one level from /home/user/docs is /home/user. Adding pictures gives /home/user/pictures.
  3. Final Answer:

    /home/user/pictures -> Option C
  4. Quick Check:

    cd ../pictures = up one + pictures folder [OK]
Hint: cd ../folder means go up then into folder [OK]
Common Mistakes:
  • Appending pictures inside current directory instead of parent
  • Going to root or home by mistake
  • Ignoring the .. and staying in same folder
4. You try to run cd myfolder but get an error: bash: cd: myfolder: No such file or directory. What is the most likely cause?
medium
A. You are already in 'myfolder' and can't cd into it again
B. You must use absolute path to change directory
C. You need to use 'cd ./myfolder' instead
D. The folder 'myfolder' does not exist in the current directory

Solution

  1. Step 1: Understand the error message

    The error says 'No such file or directory', meaning the folder named 'myfolder' is not found in the current directory.
  2. Step 2: Check the folder existence and path

    Since 'myfolder' is not present, the command fails. Using relative or absolute path won't help unless the folder exists.
  3. Final Answer:

    The folder 'myfolder' does not exist in the current directory -> Option D
  4. Quick Check:

    Error means folder missing, so cd fails [OK]
Hint: Check folder exists before cd to avoid errors [OK]
Common Mistakes:
  • Assuming cd ./myfolder fixes missing folder error
  • Thinking you can't cd into current folder
  • Believing absolute path is always required
5. You want to write a script that always starts in your home directory, then moves into a folder named projects inside it. Which sequence of commands is correct?
hard
A. cd ~\ncd projects
B. cd projects\ncd ~
C. cd /projects\ncd ~
D. cd ..\ncd projects

Solution

  1. Step 1: Start in home directory

    Using cd ~ moves the shell to the user's home directory regardless of current location.
  2. Step 2: Move into 'projects' folder inside home

    After being in home, cd projects moves into the 'projects' folder located there.
  3. Final Answer:

    cd ~\ncd projects -> Option A
  4. Quick Check:

    Home then projects folder = cd ~ then cd projects [OK]
Hint: Always cd ~ first to ensure starting at home [OK]
Common Mistakes:
  • Changing to projects before home, causing errors
  • Using absolute path /projects which may not exist
  • Using cd .. which goes up instead of home
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard