0
0
Linux CLIscripting~10 mins

mkdir (create directories) in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - mkdir (create directories)
Start
Run mkdir command
Check if directory exists
Create directory
End
The mkdir command tries to create a directory. If the directory exists, it shows an error unless options are used.
Execution Sample
Linux CLI
mkdir myfolder
mkdir myfolder
mkdir -p myfolder/subfolder
Creates a directory 'myfolder', tries to create it again (error), then creates 'myfolder/subfolder' with -p option.
Execution Table
StepCommandDirectory Exists?Action TakenOutput
1mkdir myfolderNoCreate 'myfolder'
2mkdir myfolderYesError: directory existsmkdir: cannot create directory ‘myfolder’: File exists
3mkdir -p myfolder/subfolderYes (myfolder), No (subfolder)Create 'subfolder' inside 'myfolder'
💡 After step 3, all directories exist; command ends successfully.
Variable Tracker
DirectoryStartAfter Step 1After Step 2After Step 3
myfolderDoes not existExistsExistsExists
myfolder/subfolderDoes not existDoes not existDoes not existExists
Key Moments - 2 Insights
Why does the second mkdir command give an error?
Because 'myfolder' already exists after step 1, so trying to create it again without -p causes an error (see step 2 in execution_table).
What does the -p option do in mkdir?
It creates parent directories as needed and does not error if the directory exists, allowing nested directories creation (see step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of step 2?
ANo output, directory created
BDirectory created with no error
Cmkdir: cannot create directory ‘myfolder’: File exists
DPermission denied error
💡 Hint
Check the 'Output' column for step 2 in the execution_table.
At which step does 'myfolder/subfolder' get created?
AStep 2
BStep 3
CStep 1
DIt is never created
💡 Hint
Look at the 'Directory Exists?' and 'Action Taken' columns for 'myfolder/subfolder' in the execution_table.
If we remove the -p option from step 3, what would happen?
AError because 'myfolder/subfolder' parent missing
BSubfolder is created anyway
CError because 'myfolder' does not exist
DNo error, command ignored
💡 Hint
Without -p, mkdir cannot create nested directories if parents don't exist; see step 3 explanation.
Concept Snapshot
mkdir command creates directories.
If directory exists, mkdir errors unless -p option is used.
-p creates parent directories as needed.
Syntax: mkdir [options] directory_name
Use -p for nested directories without errors.
Full Transcript
The mkdir command is used to create directories in Linux. When you run mkdir with a directory name, it creates that directory if it does not exist. If the directory already exists, mkdir shows an error unless you use the -p option. The -p option allows creating nested directories and does not error if the directory exists. For example, running 'mkdir myfolder' creates a folder named myfolder. Running it again causes an error because the folder exists. Using 'mkdir -p myfolder/subfolder' creates the subfolder inside myfolder, creating any missing parent directories as needed.