
CIS120
Linux Fundamentals
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.
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 for your class notes:
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
Let's create a folder for your project:
mkdir my_project
Creating Multiple Directories
You can create several folders at once:
mkdir notes homework projects
Creating Nested Directories
To create a folder inside another folder (even if the parent doesn't exist):
mkdir -p school/spring2024/cs101/assignments
Creating with Permissions
To create a directory with specific access rights:
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: Make sure you're creating folders where you want them
- 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