What if you could create dozens of folders in just one command, saving you hours of tedious clicking?
Why mkdir (create directories) in Linux CLI? - Purpose & Use Cases
Imagine you need to organize your photos into many folders by year and event. Doing this by clicking through your file explorer and creating each folder one by one feels like a long, boring chore.
Manually creating folders takes a lot of time and effort. It's easy to make mistakes like typos or forgetting to create some folders. If you have many folders to make, it quickly becomes frustrating and error-prone.
The mkdir -p command lets you create one or many folders quickly with just one line. You can even create nested folders all at once, saving time and avoiding mistakes.
Right-click > New Folder > Type name > Repeat many times
mkdir -p photos/2024/vacationYou can instantly set up complex folder structures, making your work organized and efficient without the hassle.
A photographer preparing folders for a client's photoshoot can create all needed directories in seconds using mkdir -p, instead of clicking through dozens of times.
Manual folder creation is slow and error-prone.
mkdir -p automates folder creation quickly and accurately.
This saves time and keeps your files well organized.
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
