Creating Directories
Directories are just containers for files. Creating them is straightforward.
Basic mkdir
The d at the start confirms it's a directory.
Create Multiple Directories
The -p Flag (Parents)
Here's where it gets useful. Without -p:
The error occurs because projects/webapp/ doesn't exist yet. With -p:
-p creates parent directories as needed. It's so useful that I almost always use it.
Always Use -p
mkdir -p never fails if the directory already exists. It's safer and more convenient. Make it your default.
Create a Standard Project Structure
Brace expansion + -p = instant project scaffolding.
More complex example:
mkdir -p project/{src/{components,utils,styles},tests/{unit,integration},docs/{api,guides}}
Create and Enter
Common pattern: create a directory and immediately cd into it:
The && Operator
&& means "run the second command only if the first succeeds." If mkdir fails, cd won't run.
Directory Permissions
New directories get default permissions based on your umask:
755 is typical: owner has full access, others can read and enter.
You can specify permissions at creation:
What mkdir Can't Do
- Won't overwrite existing directories (safe by default)
- Won't create files (use
touchfor that) - Won't set ownership (use
chownafter)
What does `mkdir -p a/b/c` do if `a/` doesn't exist?
Key Takeaways
mkdir dirnamecreates a directorymkdir -p path/to/dircreates entire directory chains-palso silently succeeds if directory already exists- Use brace expansion for complex structures:
{a,b,c} - Combine with
&&to create and enter:mkdir -p dir && cd dir
Next: copying files and directories.