mkdir (create directories) in Linux CLI - Time & Space Complexity
When creating directories using the mkdir command, it's helpful to understand how the time it takes grows as you create more directories.
We want to know how the work done by mkdir changes when the number of directories increases.
Analyze the time complexity of the following code snippet.
for dir in dir1 dir2 dir3 dir4 dir5; do
mkdir "$dir"
done
This script creates five directories one by one using a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
mkdircommand inside the loop creates one directory per iteration. - How many times: The loop runs once for each directory name, so it runs n times if there are n directories.
Each directory creation takes roughly the same amount of time, so total time grows as you add more directories.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 mkdir commands run |
| 100 | 100 mkdir commands run |
| 1000 | 1000 mkdir commands run |
Pattern observation: The total work grows directly with the number of directories you create.
Time Complexity: O(n)
This means the time to create directories grows linearly with how many directories you want to make.
[X] Wrong: "Creating multiple directories with mkdir is always instant no matter how many directories."
[OK] Correct: Each directory creation takes some time, so more directories mean more total time, even if each one is quick.
Understanding how simple commands like mkdir scale helps you think clearly about scripts and automation tasks in real work.
"What if we used mkdir -p to create nested directories in one command? How would the time complexity change?"
Practice
mkdir command do in Linux?Solution
Step 1: Understand the purpose of mkdir
Themkdircommand is used to create new directories (folders) in Linux.Step 2: Compare with other commands
Deleting files is done byrm, listing files byls, and changing directories bycd.Final Answer:
Creates new directories (folders) to organize files -> Option BQuick Check:
mkdir= create folders [OK]
- Confusing mkdir with rm or ls commands
- Thinking mkdir deletes files
- Assuming mkdir changes directories
projects?Solution
Step 1: Recall mkdir syntax
The basic syntax to create a directory ismkdir directory_name. No extra symbols or options are needed for a simple directory.Step 2: Evaluate options
mkdir -d projects uses a non-existent-dflag, mkdir: projects uses invalid colon syntax, and mkdir > projects uses redirection which is incorrect here.Final Answer:
mkdir projects -> Option CQuick Check:
Simple mkdir = mkdir directory_name [OK]
- Adding unnecessary flags like -d
- Using colons or symbols incorrectly
- Trying to redirect output with >
mkdir -p home/user/docs
Solution
Step 1: Understand the -p option
The-poption tells mkdir to create parent directories as needed without error if they already exist.Step 2: Analyze the command effect
This command creates the full pathhome/user/docsincluding any missing directories in the path.Final Answer:
Creates home, user, and docs directories if they don't exist -> Option AQuick Check:
mkdir -p= create nested folders [OK]
- Thinking -p only creates the last folder
- Assuming -p is invalid or causes errors
- Confusing mkdir with delete commands
mkdir folder1 folder2 folder3 but get an error. What is the most likely cause?Solution
Step 1: Check mkdir multiple folder support
mkdir supports creating multiple folders by listing names separated by spaces.Step 2: Identify common error with spaces
If folder names contain spaces, they must be quoted or escaped. Otherwise, mkdir treats parts as separate names causing errors.Final Answer:
One or more folder names contain spaces without quotes -> Option DQuick Check:
Spaces in names need quotes for mkdir [OK]
- Thinking -p is needed for multiple folders
- Believing mkdir only creates one folder at a time
- Not quoting folder names with spaces
src, bin, and docs inside myproject in one command. Which command achieves this?Solution
Step 1: Understand nested folder creation
Using-pallows creating parent directories and nested folders in one command without errors.Step 2: Analyze each option
mkdir myproject/src myproject/bin myproject/docs lacks-pso may fail ifmyprojectdoesn't exist. mkdir -p myproject/src myproject/bin myproject/docs correctly uses-pto create all paths. mkdir -p myproject/src/bin/docs creates nested folders incorrectly. mkdir myproject && mkdir src bin docs creates folders separately, not in one command.Final Answer:
mkdir -p myproject/src myproject/bin myproject/docs -> Option AQuick Check:
Use mkdir -p with full paths for multiple nested folders [OK]
- Omitting -p when parent folder doesn't exist
- Creating nested folders incorrectly as one path
- Running multiple mkdir commands instead of one
Ready to prove it?
Pass the test and this pattern moves unknown
