Create Nested Directory Structures
In this step, we will learn how to create nested directory structures in a single command using the -p
option with mkdir
.
Understanding the -p
Option
The -p
option (which stands for "parents") allows mkdir
to create parent directories as needed. This is useful when you want to create a deep directory structure without having to create each level manually.
Using Brace Expansion for Multiple Directories
Linux bash also supports brace expansion ({}
), which allows you to create multiple directories with similar paths in a single command.
Let's create a nested directory structure for the coral_reefs
research area:
- Use the
-p
option with brace expansion to create three subdirectories inside coral_reefs
:
mkdir -p marine_biology/coral_reefs/{images,reports,mapping_data}
- Verify the directory structure:
ls marine_biology/coral_reefs/
You should see the three new directories:
images mapping_data reports
- For even more complex structures, you can nest the brace expansions:
mkdir -p marine_biology/fish_population/{tropical,deep_water}/{surveys,samples}
- Verify this more complex structure:
ls marine_biology/fish_population/
You should see:
deep_water tropical
- Check one level deeper:
ls marine_biology/fish_population/tropical/
You should see:
samples surveys
The -p
option combined with brace expansion makes it easy to create complex directory structures with a single command, saving you time and effort when organizing large data sets.