mkdir Command
Think of the mkdir command as your digital folder creator. Just like you create folders on your desktop to organize files, mkdir helps you create directories (folders) in Linux to keep your files organized.
The examples below use the course setup. Work in ~/playground/chapter2 so you can create directories there alongside the existing practice files.
Why Use mkdir?
You'll use mkdir when you want to:
- Create a new folder for your project
- Organize your files into categories
- Set up a directory structure for a new task
- Create multiple folders at once
Basic Usage
The simplest way to create a directory is:
mkdir directory_name
For example, to create a folder in your chapter 2 playground:
cd ~/playground/chapter2
mkdir class_notes
Common Options
| Option | What It Does | When to Use It |
|---|---|---|
-p |
Creates parent directories if they don't exist | When creating nested folders |
-v |
Shows what's being created | When you want to see the process |
-m |
Sets permissions for the new directory | When you need specific access rights |
-Z |
Sets SELinux security context | When working with SELinux systems |
--context |
Like -Z but specifies full context | When you need specific SELinux settings |
--mode |
Same as -m but more explicit | When you want to be very clear about permissions |
--parents |
Same as -p but more explicit | When you want to be very clear about parent creation |
--verbose |
Same as -v but more explicit | When you want to be very clear about verbosity |
--help |
Shows help information | When you need to see all options |
--version |
Shows version information | When you need to check the version |
Practical Examples
Creating a Single Directory
From ~/playground/chapter2, create a folder for a project:
cd ~/playground/chapter2
mkdir my_project
Creating Multiple Directories
You can create several folders at once in the chapter 2 playground:
cd ~/playground/chapter2
mkdir notes homework projects
Creating Nested Directories
To create a folder inside another folder (even if the parent doesn't exist), use -p. From ~/playground/chapter2:
cd ~/playground/chapter2
mkdir -p school/spring2024/cs101/assignments
This creates school, then school/spring2024, then school/spring2024/cs101, then school/spring2024/cs101/assignments in one command.
Creating with Permissions
To create a directory with specific access rights (for example in your chapter 2 playground):
cd ~/playground/chapter2
mkdir -m 755 shared_folder
This creates a folder that you can read, write, and execute, while others can only read and execute.
Tips for Success
- Use descriptive names: Make your directory names clear and meaningful
- Use -p for nested directories: It saves time and prevents errors
- Check your current location: Use
pwdto confirm you're in~/playground/chapter2(or where you want the new folders) - Use -v when learning: It helps you understand what's happening
Common Mistakes to Avoid
- Creating directories with spaces in names (use underscores instead)
- Forgetting the -p option when creating nested directories
- Not checking if a directory already exists
- Using special characters in directory names
Best Practices
- Keep directory names short but descriptive
- Use lowercase letters for consistency
- Use underscores instead of spaces
- Create a logical folder structure