Create Nested Folders Ubuntu One Command
This guide explains how to generate complex directory structures within the Ubuntu terminal using a single command. You will learn the specific flag required to create parent and child folders simultaneously without errors. This method saves time compared to creating each folder individually and is essential for efficient file management in Linux environments.
The mkdir -p Command
To create nested folders in one step, use the mkdir
command with the -p flag. The -p option stands
for “parents.” It tells the system to create all necessary parent
directories in the specified path if they do not already exist.
Syntax and Usage
The basic syntax for creating a nested directory structure is:
mkdir -p path/to/your/foldersReplace path/to/your/folders with your desired directory
hierarchy. You can specify multiple structures in the same command by
separating them with spaces.
Practical Examples
To create a folder named project containing a subfolder
named src and another named docs, run:
mkdir -p project/src project/docsTo create a deeper hierarchy, such as
home/user/config/settings, use:
mkdir -p home/user/config/settingsIf you run this command without the -p flag and the
home or user directories do not exist, the
terminal will return an error stating that the path does not exist. The
-p flag prevents this error by building the entire
chain.
Verifying the Structure
After running the command, confirm that the directories were created
successfully using the tree command or ls:
ls -RThis lists all files and directories recursively, allowing you to see the new nested structure immediately.