When working in a Linux desktop environment like Ubuntu, creating a new folder is as easy as right-clicking and selecting “New Folder.” However, if you are working on a headless server, writing a bash script, or simply prefer the speed of the terminal, you need to know how to create folders from the command line.
In Linux terminology, folders are called “directories,” and the command used to make them is mkdir (short for “make directory”).
How to Create a Single Directory
The syntax for mkdir is incredibly straightforward.
Syntax: mkdir [directory_name]
- Open your Linux terminal.
- Navigate to the location where you want the new folder to live. (For example, type
cd Documentsand press Enter). - Type the command followed by the name you want to give the folder:
mkdir Project_Files - Press Enter.
Linux will execute the command instantly. Unless there is an error (like you don’t have permission to write to that location), mkdir is a “silent” command—it won’t output a success message. You can type ls to list the contents of your current location and verify that the “Project_Files” directory now exists.
How to Create Multiple Directories at Once
If you need to set up a workspace with several folders, you don’t need to run the command multiple times. You can simply list all the directory names separated by spaces.
mkdir Images Scripts Backups Logs
Pressing Enter will instantly create all four directories in your current location.
How to Create Nested Directories (The -p Flag)
This is where mkdir becomes truly powerful. Imagine you want to create a folder called “2024”, and inside that folder you want a folder called “October”, and inside that folder you want one called “Invoices”.
If you try to run mkdir 2024/October/Invoices, Linux will throw an error because the parent folders (2024 and October) don’t exist yet.
You can force mkdir to automatically create all missing parent directories by adding the -p (parents) flag.
mkdir -p 2024/October/Invoices
This single command will seamlessly build the entire three-level directory tree from scratch. This is an incredibly useful trick when writing automated backup scripts.
Dealing with Spaces in Directory Names
Linux terminal commands use spaces to separate arguments. Because of this, if you type mkdir My Summer Vacation, Linux thinks you are asking it to create three separate folders: one called “My”, one called “Summer”, and one called “Vacation”.
If you want a single folder with spaces in the name, you must wrap the name in quotation marks:
mkdir "My Summer Vacation"