LEARN THE MKDIR COMMAND IN UNDER 1 MINUTE #LINUX #SHORTS
Enjoying this content? Subscribe to the Channel!
Mastering the Linux Filesystem: Your Complete Guide to the mkdir Command
Introduction: Why Directory Management Matters
Welcome back to Darren’s Tech Tutorials!
If you’ve spent any time in the world of Linux, you know that the filesystem is the backbone of your entire operating environment. Being able to organize, manage, and structure your files efficiently is absolutely critical—and it all starts with creating directories.
Today, we are diving deep into one of the most fundamental commands in Linux: mkdir (Make Directory). While it might seem simple on the surface, mkdir has powerful options that can save you time, manage complex nested structures, and even set security permissions right from the moment of creation.
Let’s stop clicking around in graphical interfaces and start taking control directly from the command line!
The Basics: Creating Your First Directory
At its heart, the mkdir command is designed for simplicity. To create a new directory in your current working location, you simply type the command followed by the desired directory name.
Command Syntax
mkdir [directory_name]
Practical Example
Let’s say you are setting up a new space for all your scripting projects.
mkdir Scripts
If the command executes successfully, you won’t see any output—which is standard for Linux commands indicating success. You can verify the new directory using the ls command.
Efficiency Boost: Creating Multiple Directories at Once
Need to set up several new folders for a project, like images, docs, and assets? You don’t have to run mkdir three times! You can list multiple directory names separated by spaces.
Practical Example
To set up the foundational folders for a new web project:
mkdir project_name images docs assets
This single command instantly creates all three directories within project_name (assuming you cd into it first) or in your current location.
Handling Complexity: Creating Nested Directories with the -p Flag
One of the most common stumbling blocks for new users is trying to create a directory deep inside a path where the parent directories don’t yet exist.
For example, trying to create 2024/Q4/Final when the 2024 directory hasn’t been created yet will usually result in an error:
mkdir 2024/Q4/Final
# Output: mkdir: cannot create directory ‘2024/Q4/Final’: No such file or directory
This is where the powerful -p (parents) flag comes in.
The -p Flag Explained
The -p option tells mkdir to create any necessary parent directories along the way. If the parent directory already exists, it simply moves on. If it doesn’t, mkdir creates it first.
Practical Example
To create the entire structure successfully, regardless of whether 2024 or Q4 exists:
mkdir -p 2024/Q4/Final
This command automatically creates the 2024 directory, then the Q4 directory inside it, and finally the Final directory inside Q4. This is a huge time saver when setting up deep file structures!
Security First: Setting Permissions with the -m Flag
Linux file permissions are crucial for security. Normally, you create a directory and then immediately use the chmod command to adjust its permissions. The -m (mode) flag lets you skip that second step by setting the permissions right when you create the directory.
The permissions are specified using octal notation (e.g., 777, 755, 644).
- The first digit relates to the User (owner).
- The second digit relates to the Group.
- The third digit relates to Others.
A common permission for a standard directory is 755, which means the owner has full read, write, and execute permissions, while the group and others only have read and execute access.
Practical Example
Let’s create a directory called confidential_data and ensure only the owner can write to it:
mkdir -m 755 confidential_data
This creates the directory and immediately sets its permissions to 755, adding an immediate layer of security to your new folder.
Note on Ownership: While you can set permissions with mkdir -m, setting the ownership (who the user and group are) requires the chown command after the directory is created.
Conclusion: Take Control of Your Filesystem!
The mkdir command is far more than just a basic utility. By leveraging the -p and -m flags, you can quickly and efficiently build complex, secure directory structures right from the command line.
You are now equipped to:
- Create basic directories.
- Batch-create multiple directories instantly.
- Build deep nested paths using the
mkdir -pcommand. - Set initial security permissions using
mkdir -m.
I encourage you to open your terminal right now and start practicing these commands. The more comfortable you get with these fundamentals, the faster and more proficient you’ll become in Linux!
If this tutorial helped you level up your command line skills, please hit the Like button, Subscribe to Darren’s Tech Tutorials for more practical guides, and let me know in the comments below which Linux command you want me to tackle next! Happy coding!