0
0
Linux CLIscripting~10 mins

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

Choose your learning style9 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.