Create a Directory Structure
Create a Directory Structure
A school might organize reports by term: a school folder with term1 inside it. A directory stores entries such as filenames and subdirectory names, helping you arrange a useful hierarchy. You create a directory with mkdir, not with the file-creation command touch.
There is one practical wrinkle: the parent folder may not exist yet. We will use the parent-creation option so the command can build the path from the missing components, rather than expecting every level to have been prepared earlier.
What you’ll learn
- Create a directory and missing parent directories.
- Use mkdir -p when the required parent may not exist.
A nested directory needs its parent first
Desired path
The requested term folder sits inside school.
school/term1Create parents
mkdir -p creates missing components along the path.
school/ → term1/Verify
test -d checks that the final path is a directory.
term1 is ready
Create the school and term directories together
Prepare the example
Begin in a fresh folder you own. We want school/term1 beneath that folder. Read the slash as the boundary between a parent directory name and its child; it is not part of either individual name.
Bash · Linux terminal
mkdir -p school/term1
test -d school/term1 && printf 'Term directory ready\n'Expected result
Term directory readymkdir -p school/term1 creates missing parent directories and the final directory. Without -p, asking for a child beneath a missing parent would fail. The option also accepts an already-existing directory path rather than treating that alone as an error.
The second line checks the final path with test -d. Its message appears only when that path is a directory. If creation fails, inspect the diagnostic and the parent path: a regular file occupying a component name, or lack of access to the parent, can prevent the operation. Adding more options will not fix a path that describes a different place from the one you intended.
Single folders, several folders and missing parents
A Linux directory is a folder that can contain files and other directories. The command used to create one is mkdir, short for “make directory.”
What is a Directory?
A directory is like a container where you can organize files and other directories. It's just a fancy word for a folder in Linux.
Open a terminal where you want the directory
Whether you're using Ubuntu, Fedora, or any other Linux distribution, open your terminal window. We're going to use the mkdir command — which stands for "make directory".
mkdir syntax for creating directories
mkdir <directory-name>
Let’s try a basic example. Suppose you want to create a folder called projects.
Example 1: Creating a Single Directory
mkdir projects
No output means it worked!
Now if you type:
ls
projects
You’ll see your new folder listed.
Example 2: Creating Multiple Directories
You can create more than one directory at a time by listing them in the mkdir command:
mkdir images videos documents
No output means all 3 were created!
Example 3: Creating Nested Directories
Want to create a folder inside another folder? Use the -p flag (this stands for "parent"):
mkdir -p tutorials/linux/basics
No errors? Your nested folders are all ready!
Without -p, you’d get an error if the parent folders don’t exist. So -p is super helpful.
Check It Out with Tree
If you have the tree command installed, you can see your directory structure visually:
tree tutorials
tutorials
└── linux
└── basics
Build directory paths with mkdir
mkdiris used to make directories-plets you create nested directories- You can create multiple directories at once
Try it, then check the worked answer
Your task: Create term1 when school may not exist.
Begin in a fresh folder you own. We want school/term1 beneath that folder. Read the slash as the boundary between a parent directory name and its child; it is not part of either individual name.
Bash · complete example for this question
mkdir -p school/term1
test -d school/term1 && printf 'Term directory ready\n'Expected result
Term directory readyWorked answer
mkdir -p school/term1
Choose mkdir -p school/term1 when school may be missing. touch creates regular files, so it is not the right way to build this directory hierarchy. You can confirm the result by listing the school folder or checking the final directory path.
Check your decision
Use -p when parent directories may be missing
Use mkdir to create directory entries and -p when missing parents are part of the task. A nested path still starts from your current directory unless it is absolute. Verify the final directory before writing files beneath it.
Reaching the recap and completing the quizzes are separate milestones.