Mkdir Command
Create Directories with Mkdir
The mkdir command is a fundamental utility in Linux and
Unix-like operating systems used to create new directories
(folders). It's an essential tool for organizing files and managing
the file system structure from the command line.
Mkdir Command Usage Examples
Here are some common ways to use the mkdir command:
Creating Nested Directories
The -p option is crucial for creating parent
directories as needed. This allows you to create a directory
structure in a single command.
# To create nested directories:
mkdir -p foo/bar/baz
Creating Multiple Directories
You can create multiple directories within a parent directory using brace expansion.
# To create foo/bar and foo/baz directories:
mkdir -p foo/{bar,baz}
Creating Complex Directory Structures
Brace expansion can be combined to create more intricate directory layouts.
# To create the foo/bar, foo/baz, foo/baz/zip and foo/baz/zap directories:
mkdir -p foo/{bar,baz/{zip,zap}}
Understanding Mkdir Options
While the examples above cover common use cases,
mkdir has other options. The -p (parents)
option is the most frequently used, ensuring that intermediate
directories are created if they don't exist. Without
-p, mkdir would fail if the parent
directory for the new directory does not exist.
Related Commands and Resources
- File System Navigation: Learn more about navigating file systems.
- GNU Coreutils Manual - mkdir: Official documentation for the mkdir command.
- Linux Command Line on Stack Overflow: Find answers to common command-line questions.