Creating a directory is one of the first tasks anyone learns when navigating the Linux command line. Whether you are a system administrator managing servers, a developer organizing code repositories, or a student learning the basics of the operating system, mastering the mkdir command is fundamental. This guide provides a comprehensive walkthrough of how to make a folder in Linux, covering basic syntax, advanced options, permission handling, and practical examples to streamline your workflow.
Understanding the mkdir Command
The primary tool for creating directories in Linux is mkdir, short for "make directory.In practice, " It is a standard utility available across all major distributions, including Ubuntu, Debian, CentOS, Fedora, and Arch. Unlike graphical file managers where you right-click and select "New Folder," the terminal offers granular control over naming conventions, parent directory creation, and permission settings simultaneously.
The basic syntax follows this structure:
mkdir [OPTIONS] DIRECTORY_NAME...
You can create a single folder or multiple folders in a single execution. The command is silent on success—returning you to the prompt without output—unless an error occurs (such as the directory already existing) or you invoke verbose mode.
Creating a Single Directory
To create a basic folder in your current working directory, simply type mkdir followed by the desired name Not complicated — just consistent..
mkdir projects
If you list the contents of the directory using ls -l, you will see projects listed with default permissions (typically drwxr-xr-x). The d at the beginning of the permission string signifies that this is a directory And that's really what it comes down to..
Handling Spaces and Special Characters Linux filenames are case-sensitive and can contain spaces, though spaces require escaping or quoting to prevent the shell from interpreting them as separate arguments.
# Using quotes (recommended for readability)
mkdir "My Documents"
# Using escape characters
mkdir My\ Documents
Both commands create a single folder named My Documents. Using quotes is generally preferred in scripts and daily usage to avoid accidental argument splitting.
Creating Multiple Directories Simultaneously
Efficiency in the terminal often means reducing keystrokes. You can create several folders at once by listing their names separated by spaces.
mkdir folder1 folder2 folder3
This command generates three distinct directories in the current location. This is particularly useful when scaffolding a project structure, such as creating standard directories for a web application:
mkdir css js images assets fonts
Building Nested Directory Structures with -p
One of the most powerful features of mkdir is the -p (parents) flag. By default, mkdir fails if you try to create a subdirectory inside a parent that does not exist.
# This fails if 'parent' does not exist
mkdir parent/child
# Output: mkdir: cannot create directory 'parent/child': No such file or directory
Using the -p flag tells Linux to create the entire hierarchy, including any missing parent directories, without throwing an error if they already exist.
mkdir -p parent/child/grandchild
This single command creates parent, then child inside it, and finally grandchild inside child. This flag is indispensable for scripting and automation, ensuring idempotency—running the command multiple times produces the same result without errors.
Combining -p with Brace Expansion
For complex project scaffolding, combine the -p flag with brace expansion (a feature of Bash, Zsh, and other modern shells). The result? You get to define a tree structure in one line Worth keeping that in mind. That alone is useful..
mkdir -p project/{src,bin,docs,tests/{unit,integration}}
This expands to create:
project/srcproject/binproject/docsproject/tests/unitproject/tests/integration
This technique transforms a tedious, multi-step process into a single, reproducible command.
Setting Permissions at Creation with -m
By default, new directories inherit permissions based on the system's umask value (usually 0022, resulting in 755 or rwxr-xr-x). Because of that, this accepts octal notation (e. Still, you can explicitly define the mode (permissions) at the moment of creation using the -m (mode) flag. Plus, g. , 700, 755, 777) or symbolic notation (e.g., u=rwx,g=rx,o=).
Common Use Cases for -m
1. Private Directory (Owner Only) Create a folder accessible only by the user who created it.
mkdir -m 700 private_data
# Permissions: drwx------
2. Shared Group Directory Create a folder where the owner and group have full access, but others have none.
mkdir -m 770 team_share
# Permissions: drwxrwx---
3. Public Drop Box (Sticky Bit Context)
While mkdir sets standard bits, creating a "drop box" where users can write but only delete their own files usually requires chmod +t afterwards. That said, you can set the base permissions immediately:
mkdir -m 1777 public_drop
# Permissions: drwxrwxrwt (The 't' is the sticky bit)
Using -m saves the extra step of running chmod immediately after creation, which is cleaner in scripts And that's really what it comes down to..
Verbose Output with -v
When creating many directories—especially within scripts or when using brace expansion—it is helpful to see confirmation of what was actually created. The -v (verbose) flag prints a message for every directory created.
mkdir -pv project/{src,bin,docs}
Output:
mkdir: created directory 'project'
mkdir: created directory 'project/src'
mkdir: created directory 'project/bin'
mkdir: created directory 'project/docs'
This provides an audit trail, confirming that the command executed exactly as intended.
Managing Permissions and Ownership Post-Creation
While mkdir -m sets initial permissions, real-world administration often requires adjusting ownership (chown) and group access (chgrp) after the fact.
Changing Ownership
To assign a directory to a specific user and group:
sudo chown www-data:www-data /var/www/html/new_site
Setting the Setgid Bit for Collaboration
A critical concept for shared team folders is the Setgid (Set Group ID) bit. When set on a directory (chmod g+s), any new files or subdirectories created inside it inherit the directory's group ownership rather than the creating user's primary group. This ensures seamless collaboration The details matter here. Which is the point..
mkdir shared_project
sudo chgrp developers shared_project
sudo chmod 2775 shared_project # 2 = setgid, 775 = rwxrwxr-x
Now, any user in the developers group can create files in shared_project, and those files will automatically belong to the developers group Easy to understand, harder to ignore..
Practical Examples and Workflows
Scenario 1: Standard Web Project Scaffold
A developer needs a standard structure for a new site Not complicated — just consistent..
mkdir -pv ~/websites/my-new-site/{public,logs,backups,config,src/{components,utils,styles}}
Scenario 2: Date-Stamped Backup Directory
Automating backups often requires folders named with the current date Most people skip this — try not to..
mkdir -p ~/backups/$(date +%Y-%m-%d)
# Creates: ~/backups/2023-10-27
Scenario 3: Secure Configuration Directory
Storing sensitive configuration files (like .env or SSH keys) requires restrictive permissions immediately.
mkdir -m 700 ~/.config/secrets