Knowing how to create directories in Linux is a fundamental skill for anyone working with servers, desktops, containers, or automation scripts. Whether you’re organizing application logs on Ubuntu, building a deployment structure on RHEL, or preparing a backup path on Debian, the mkdir command is the standard and most reliable way to create directories from the terminal. In this tutorial, you’ll learn mkdir syntax, how to create single and multiple folders, how to build nested directory trees with parent directories, how to safely handle names with spaces, and how to set permissions at creation time. Each section includes clear command examples with realistic output so you can quickly validate results and avoid common mistakes like “Permission denied” or “File exists.”
Understanding the mkdir Command (What It Does and When to Use It)
mkdir stands for “make directory.” It creates one or more directories (folders) in the filesystem. You’ll typically use it when provisioning a server, preparing an application directory layout (for example, /var/www), building a user workspace in /home, or creating paths for scripts that write output. While graphical file managers can create folders, the command line is faster, scriptable, and consistent across distributions such as Debian, Ubuntu, RHEL, CentOS, Rocky Linux, AlmaLinux, and Arch Linux.
mkdir Syntax (Basic Command Structure)
The general syntax is simple: you provide one or more directory names, plus optional flags. Most real-world usage revolves around three core features: creating nested paths with -p, setting permissions with -m, and showing what was created with -v.
mkdir --help Usage: mkdir [OPTION]... DIRECTORY... Create the DIRECTORY(ies), if they do not already exist.
The –help flag prints built-in help, including common options and usage. The output confirms that mkdir accepts multiple directory arguments and can be controlled through options.
How to Create a New Directory in the Current Working Directory
If you provide only a directory name (without a path), mkdir creates it in your current working directory. This is the most common usage when you’re already inside the target location using cd.
mkdir newdir $ ls -ld newdir drwxrwxr-x 2 username username 4096 Mar 7 12:41 newdir
This command creates a directory named newdir. The output (from a verification listing) shows the directory permissions, owner, group, size, and modification time. If you run this as a regular user, the directory is created with default permissions influenced by your system’s umask.
Creating a Directory Using an Absolute or Relative Path
To create a directory somewhere else, specify its path. An absolute path begins at the filesystem root (/), such as /tmp/newdir. A relative path is based on your current working directory, such as ./newdir or projects/newdir.
mkdir /tmp/newdir $ ls -ld /tmp/newdir drwxrwxr-x 2 username username 4096 Mar 7 12:43 /tmp/newdir
This creates /tmp/newdir. No special flags are required because /tmp typically allows users to create their own directories. The output confirms the directory exists at the specified absolute path.
Handling Permission Denied Errors (When You Need sudo)
On Linux, system directories such as /root, /etc, and often parts of /var require elevated privileges. If you attempt to create a directory in a location where you lack write permissions, mkdir fails with a “Permission denied” error.
mkdir /root/newdir mkdir: cannot create directory '/root/newdir': Permission denied
The error indicates your user cannot write to /root. On a server, you would typically use sudo (if authorized) or choose a directory you own, such as your home directory. This is expected behavior and a key part of Linux’s security model.
Verbose Mode: Print Confirmation for Each Created Directory
When scripting or troubleshooting, it’s useful to see exactly what mkdir created. Verbose mode prints a confirmation message for each directory created.
mkdir -v logs mkdir: created directory 'logs'
The -v flag enables verbose output. The message confirms that the directory logs was created successfully, which is especially helpful when creating multiple directories at once.
How to Create Parent Directories (Nested Paths) with -p
A common issue occurs when you try to create a deeply nested path where intermediate directories do not exist. Without special handling, mkdir will fail. The -p option (“parents”) tells mkdir to create any missing parent directories automatically.
mkdir /home/linuxize/Music/Rock/Gothic mkdir: cannot create directory '/home/linuxize/Music/Rock/Gothic': No such file or directory
This failure happens because one or more parent directories (for example, /home/linuxize/Music or /home/linuxize/Music/Rock) do not exist. mkdir cannot create the final directory unless the full path is already present.
mkdir -p /home/linuxize/Music/Rock/Gothic $ ls -ld /home/linuxize/Music /home/linuxize/Music/Rock /home/linuxize/Music/Rock/Gothic drwxrwxr-x 3 linuxize linuxize 4096 Mar 7 12:48 /home/linuxize/Music drwxrwxr-x 3 linuxize linuxize 4096 Mar 7 12:48 /home/linuxize/Music/Rock drwxrwxr-x 2 linuxize linuxize 4096 Mar 7 12:48 /home/linuxize/Music/Rock/Gothic
The -p flag creates missing parent directories as needed. The verification output shows that each level of the directory tree exists. Another important behavior: with -p, mkdir does not complain if the directory already exists, which makes it safe for repeated runs in scripts.
What Happens If the Directory Already Exists?
If you try to create a directory that already exists without -p, mkdir returns an error and a non-zero exit status. This is useful when you want to detect unexpected state in automation.
mkdir newdir mkdir: cannot create directory 'newdir': File exists
The output indicates the directory already exists. If your goal is “ensure the directory exists,” use -p instead, because it will succeed whether the directory is present or not.
How to Set Directory Permissions at Creation Time with -m
By default, new directories are created with permissions derived from the system’s defaults and your umask. If you need a specific permission mode immediately (common for private directories, shared collaboration folders, or application runtime directories), use -m.
mkdir -m 700 private $ ls -ld private drwx------ 2 username username 4096 Mar 7 12:52 private
The -m flag sets the permission mode, similar to chmod. Mode 700 means only the owner can read, write, and access (execute) the directory. The output shows drwx------, confirming that group and others have no access.
Creating Multiple Directories in One Command
mkdir can create multiple directories by listing them as separate arguments. This is a convenient way to build a small project layout quickly.
mkdir src bin docs $ ls -ld src bin docs drwxrwxr-x 2 username username 4096 Mar 7 12:54 bin drwxrwxr-x 2 username username 4096 Mar 7 12:54 docs drwxrwxr-x 2 username username 4096 Mar 7 12:54 src
This single command creates three directories: src, bin, and docs. The output confirms they were created with the same default permission set (subject to umask).
Creating Complex Directory Trees with Brace Expansion (Bash/Zsh)
On most Linux shells (especially Bash and Zsh), you can combine mkdir with brace expansion to build directory trees quickly. This is extremely useful for structuring media libraries, code repositories, or configuration layouts. Note that brace expansion is a shell feature, not a mkdir feature, so it depends on your shell.
mkdir -p Music/{Jazz/Blues,Folk,Disco,Rock/{Gothic,Punk,Progressive},Classical/Baroque/Early}
$ find Music -type d | sort
Music
Music/Classical
Music/Classical/Baroque
Music/Classical/Baroque/Early
Music/Disco
Music/Folk
Music/Jazz
Music/Jazz/Blues
Music/Rock
Music/Rock/Gothic
Music/Rock/Progressive
Music/Rock/Punk The -p flag ensures parent directories are created. The brace expansion generates multiple paths in one command, and the output (a directory listing) shows the resulting tree. This approach is fast, repeatable, and ideal for automation.
Directory Names with Spaces and Special Characters
Linux allows spaces in filenames and directory names, but the shell treats spaces as separators between arguments. To create a directory with spaces, you must quote the name or escape the spaces.
mkdir "My Documents" $ ls -ld "My Documents" drwxrwxr-x 2 username username 4096 Mar 7 12:57 My Documents
Quoting ensures the shell passes the directory name as a single argument. The output confirms the directory is created exactly as intended, including the space.
mkdir My\ Documents $ ls -ld "My Documents" drwxrwxr-x 2 username username 4096 Mar 7 12:58 My Documents
Escaping the space with a backslash is another correct method. Both approaches are widely used; quoting is often easier when names include multiple spaces or special characters.
Create a Directory with a Date or Time Stamp (Great for Backups)
For backup jobs and log archives, it’s common to create directories named with the current date (or date and time). You can do this using command substitution, where the shell replaces $(...) with the command’s output.
mkdir "backup-$(date +%F)" $ ls -ld backup-* drwxrwxr-x 2 username username 4096 Mar 7 13:00 backup-2026-03-07
The directory name includes the current date in ISO format (YYYY-MM-DD). This naming pattern sorts naturally and is ideal for cron jobs, retention policies, and quick human inspection. The output demonstrates a realistic created directory name.
How to Remove Directories (rmdir vs rm -r)
Creating directories is only half the story; administrators also need to remove them safely. Use rmdir for empty directories, and use rm -r when you must remove a directory and its contents. Be careful with recursive deletion on production systems.
rmdir emptydir $ ls -ld emptydir ls: cannot access 'emptydir': No such file or directory
rmdir removes only empty directories. The output indicates the directory no longer exists, which is the expected verification result. If emptydir contains files, rmdir will fail instead of deleting data.
rm -r oldproject $ ls -ld oldproject ls: cannot access 'oldproject': No such file or directory
The -r flag tells rm to remove directories and their contents recursively. The output shows that oldproject is gone. On servers, consider adding interactive mode (-i) or using safer workflows when deleting important data.
Quick Reference: Common mkdir Commands
Use this quick reference when you need a fast reminder during administration, scripting, or troubleshooting.
- Create one directory:
mkdir dirname - Create multiple directories:
mkdir dir1 dir2 dir3 - Create nested directories safely:
mkdir -p path/to/dir - Create a directory with specific permissions:
mkdir -m 755 dirname - Verbose output:
mkdir -v dirname
Conclusion
The mkdir command is the standard tool to create directories in Linux, and it’s essential for day-to-day server administration, scripting, and system organization. For most workflows, remember these best practices: use -p to create parent directories and make scripts idempotent, use -m to enforce permission requirements at creation time, and use -v when you want confirmation of what was created. Mastering these options helps you build clean directory structures on any Linux distribution while avoiding common errors related to missing paths, existing directories, or insufficient permissions.