How to Create a Directory in Linux: Commands, Options, and Best Practices
Creating directories in Linux is one of the most fundamental file system operations — and while the basic command takes just a few seconds to learn, there's more depth here than most tutorials cover. Understanding the full toolset helps you work efficiently whether you're organizing a home folder, setting up server paths, or scripting automated workflows.
The Core Command: mkdir
The primary tool for creating directories in Linux is mkdir (short for make directory). At its simplest:
mkdir foldername This creates a new directory called foldername in your current working location. If you're not sure where you currently are in the file system, run pwd (print working directory) first.
To create a directory in a specific path rather than your current location:
mkdir /home/user/documents/projects This works as long as every parent directory in that path (home, user, documents) already exists.
Creating Nested Directories with -p
One of the most useful mkdir flags is -p (or --parents). It creates the full path you specify, including any intermediate directories that don't yet exist — without throwing an error if they already do.
mkdir -p /home/user/projects/2024/reports Without -p, if /projects/2024/ doesn't exist yet, the command fails. With -p, Linux builds the entire directory tree in one step. This is particularly valuable in shell scripts where you can't always predict the existing file structure.
Creating Multiple Directories at Once
mkdir accepts multiple arguments in a single command:
mkdir photos videos documents This creates three separate directories inside your current location simultaneously. You can combine this with -p as well:
mkdir -p project/{src,bin,docs,tests} This brace expansion syntax (a shell feature, not mkdir itself) creates four subdirectories under project/ in one line. It's a common pattern in development environments and is supported in Bash, Zsh, and most modern Linux shells.
Setting Permissions at Creation: -m
By default, a new directory inherits permissions based on your system's umask value — typically resulting in 755 permissions (owner can read/write/execute; group and others can read/execute).
If you need specific permissions from the start, use the -m flag:
mkdir -m 700 private_folder This creates the directory with 700 permissions, meaning only the owner can access it. This is useful when creating directories intended to hold sensitive files like SSH keys or configuration data.
You can still change permissions afterward with chmod, but setting them at creation is cleaner in scripted or automated contexts.
Verifying the Directory Was Created
After running mkdir, a quick confirmation step:
ls -la This lists all contents of the current directory with details including permissions, ownership, and timestamps. New directories appear with a d at the start of their permission string (e.g., drwxr-xr-x).
Alternatively, use:
ls -ld /path/to/directory The -d flag shows information about the directory itself rather than its contents.
Common mkdir Options at a Glance 📋
| Flag | Long Form | What It Does |
|---|---|---|
-p | --parents | Creates parent directories as needed; no error if exists |
-m | --mode | Sets permission mode at creation |
-v | --verbose | Prints a message for each directory created |
| (none) | (none) | Creates a single directory in the current path |
The -v flag is especially handy when using -p across a deep path — it confirms each directory as it's created, which helps with troubleshooting.
Paths: Absolute vs. Relative
Understanding absolute versus relative paths matters more as your directory structures grow.
- Absolute path: Starts from the root (
/). Always the same regardless of where you are. Example:/var/www/html/assets - Relative path: Based on your current directory. Example:
mkdir ../backupcreates a directory one level up from where you are.
Using absolute paths in scripts is generally safer — relative paths can produce unexpected results if the script is called from a different working directory.
When Things Go Wrong
A few common errors and what they mean:
mkdir: cannot create directory 'name': File exists— The directory already exists. Use-pto suppress this error in scripts.mkdir: cannot create directory '/path/folder': No such file or directory— A parent directory in your path doesn't exist yet. Add-pto create the full chain.mkdir: cannot create directory '/etc/newdir': Permission denied— You don't have write access to that location. Prependsudoif appropriate, or check directory ownership withls -la.
Variables That Affect Your Approach 🔧
How you create directories depends on several factors specific to your situation:
- Interactive use vs. scripting: Manual terminal use is forgiving; scripts need flags like
-pto handle edge cases without failing. - Permission requirements: Shared servers, multi-user systems, and security-sensitive environments require intentional permission settings from the start.
- Shell type: Brace expansion syntax works in Bash and Zsh but behaves differently in older shells like
shordash. - Linux distribution: The
mkdircommand itself is consistent across distributions (it's part of GNU coreutils), but default umask values and filesystem mount options vary by distro and system configuration. - Privilege level: Creating directories inside system paths (
/etc,/var,/usr) requires root or sudo access. User-owned paths under/homegenerally don't.
The right combination of flags, paths, and permissions depends on whether you're working on a personal machine, a shared development server, a containerized environment, or an automated deployment pipeline — and each of those contexts calls for a different approach.