For our Blog Visitor only Get Additional 3 Month Free + 10% OFF on TriAnnual Plan YSBLOG10
Grab the Deal

Mkdir Command in Linux Explained With Examples in 2026

The mkdir command in Linux creates new directories. Its basic form is mkdir DIRECTORY, and you can create multiple folders at once, set permissions with -m, build nested paths with -p, and print output with -v.

It respects your umask, returns errors if a path exists without -p, and supports advanced patterns like brace expansion. Creating directories is one of the first things you do on any Linux system.

In this guide, we’ll explain the mkdir command in Linux with clear examples, best practices, and troubleshooting tips. By the end, you’ll confidently create single, multiple, and nested directories with the right permissions, whether locally or on a server at YouStable.


What is mkdir in Linux?

mkdir (make directory) is a core Linux utility that creates one or more directories. It’s part of GNU coreutils on most distributions and is also available in BSD variants (macOS). It’s often used in deployment scripts, CI/CD pipelines, and everyday file management.

Syntax and Common Options

Here’s the standard syntax and the options you’ll use most:

mkdir [OPTIONS] DIRECTORY [DIRECTORY...]
# Common options:
# -p, --parents        Create parent directories as needed, no error if existing
# -m, --mode=MODE      Set permissions on the new directory (e.g., 755 or u=rwx,go=rx)
# -v, --verbose        Print a message for each created directory
# -Z, --context=CTX    Set SELinux security context (on SELinux-enabled systems)

Basic Usage Examples

Create a Single Directory

mkdir reports

If “reports” already exists, mkdir returns an error unless you use -p (explained below).

Create Multiple Directories at Once

mkdir january february march

This creates three directories in the current working path. You can also use absolute paths.

Create Nested Directories with -p

Use -p to create parent directories as needed and avoid errors if paths already exist.

mkdir -p /var/www/project/{public,logs,backup}

Brace expansion ({…}) is handled by your shell (e.g., bash), not mkdir. It expands into three directories under /var/www/project.


Setting Permissions with -m (and Understanding umask)

By default, new directory permissions are influenced by your umask. Typical defaults are 022 or 002 depending on the distro and policy. You can check your current umask with:

umask

Set Numeric Permissions

# 755 = owner rwx, group rx, others rx
mkdir -m 755 app

# 700 = owner rwx only
mkdir -m 700 secrets

Set Symbolic Permissions

mkdir -m u=rwx,go=rx shared
mkdir -m u=rwx,g=rx,o= private_team

Note: The -m value overrides the default umask for that command only. If you need to adjust defaults system wide, modify your shell or PAM configuration, then relogin.


Verbose Output and Absolute vs. Relative Paths

Use -v to Confirm What’s Created

mkdir -vp /opt/tools/bin
# Output: mkdir: created directory '/opt/tools'
#         mkdir: created directory '/opt/tools/bin'

Absolute vs. Relative Paths

Absolute paths start from / and are unambiguous (/data/backups). Relative paths depend on your current directory (./backups or just backups). For scripts and automation, prefer absolute paths to reduce errors.


Handling Errors: File Exists, Permission Denied, and More

mkdir provides helpful errors. Here are common ones and quick fixes:

  • File exists: The directory already exists. Use -p to avoid errors when re-running scripts.
  • Permission denied: You lack rights to the parent path. Switch to a writable directory, adjust ownership/permissions, or use sudo if appropriate.
  • No such file or directory: The parent path doesn’t exist. Use -p to create it.
  • No space left on device: The filesystem is full. Free space or extend storage.
# Robust pattern for idempotent script runs
mkdir -p /srv/app/logs || { echo "Failed to create logs"; exit 1; }

Real World Use Cases (Hosting and DevOps)

  • Web roots: mkdir -p /var/www/example.com/{public_html,logs,tmp}
  • Deployment pipelines: Prepare release folders, artifacts, and cache directories safely with -p.
  • Backups: mkdir -p /backups/$(date +%F) to snapshot daily backup directories.
  • Multi tenant hosting: Create per-site directories with correct permissions and ownership.

If you’re deploying on YouStable VPS or Cloud hosting, you can SSH into your server and use mkdir to prepare docroots, logs, and isolation-friendly paths. Our support can guide you on secure permissions for NGINX/Apache and PHP-FPM pools.


Advanced Patterns and Productivity Tips

Create Many Directories with Brace Expansion

# Monthly data directories
mkdir -p data/{2024,2025}/{01..12}

# App structure scaffold
mkdir -p app/{bin,config,lib,log,tmp}

From a File or Command Stream

# Create directories listed in a file
xargs -I {} mkdir -p "{}" <<EOF
/var/www/site1/logs
/var/www/site2/logs
/var/www/site3/logs
EOF

# With find to mirror a structure
find templates -type d -printf '%P\n' | xargs -I {} mkdir -p "output/{}"

SELinux Contexts (SELinux Enabled Systems)

On SELinux systems, you can apply contexts at creation time in some setups:

# Set default context for created dirs (policy-dependent)
mkdir -Z -p /var/www/example.com/public_html

# Or specify an explicit context if supported
mkdir --context=system_u:object_r:httpd_sys_content_t:s0 /var/www/example.com/public_html

Always verify with ls -Z and consult your security policy. In many cases, semanage fcontext and restorecon are preferred for persistent labeling.

Security and Permission Best Practices

  • Avoid 777: World writable directories invite risk. Prefer 750/755 for shared reads and 700 for private data.
  • Use groups: Create a group for a project and set directory group ownership for collaborative write access.
  • Sticky bit for shared temp: For shared writable dirs (like /tmp), sticky bit prevents file deletion by others.
  • Ownership matters: After mkdir, assign correct owner and group with chown to the service or deployment user.
# Example: secure web root with group collaboration
sudo mkdir -p /var/www/project/{public,logs}
sudo chown -R deploy:www-data /var/www/project
sudo chmod -R 750 /var/www/project
sudo chmod 2750 /var/www/project # setgid to inherit group

On YouStable servers, our team can review your directory layout, permissions, and umask to ensure security and consistent deployments across environments.

Troubleshooting Checklist

  • Check path: pwd and ls -ld parent directories to confirm correct location.
  • Verify permissions: ls -ld parent and identify whether you need sudo or chown.
  • Confirm umask: umask may be stripping intended permissions; use -m for exact mode.
  • Re-run safely: Use mkdir -p to prevent errors on existing paths in scripts.
  • Audit SELinux: On policy-enforced systems, check contexts with ls -Z.

Command Recap: Quick Examples

# Basic
mkdir test

# Multiple
mkdir dir1 dir2 dir3

# Nested parents
mkdir -p /data/app/releases/current

# Verbose create
mkdir -vp /opt/tools/bin

# With permissions
mkdir -m 755 public
mkdir -m 700 private

# With brace expansion
mkdir -p /var/www/site/{public_html,logs,tmp}

FAQ’s

1. What does the mkdir command do in Linux?

mkdir creates directories. You can create one or multiple at a time, build nested directories with -p, set permissions with -m, and show progress with -v. It respects your system’s umask unless you explicitly set a mode.

2. How do I create nested directories in one command?

Use mkdir -p. For example: mkdir -p /var/www/project/public_html creates all missing parent directories without errors if they already exist.

3. How do I set directory permissions during creation?

Use -m. For numeric: mkdir -m 755 docs. For symbolic: mkdir -m u=rwx,go=rx docs. This overrides the current umask for that command.

4. How can I create multiple directories at once?

List them: mkdir img css js. For patterns, use brace expansion: mkdir -p env/{dev,stage,prod}/logs to create nine paths in a single command.

5. Why does mkdir say “Permission denied”?

You don’t have rights to the parent directory. Create paths under your home, change ownership or permissions, or use sudo when appropriate (e.g., system paths like /var or /opt).


Conclusion

The mkdir command in Linux is simple yet powerful. With -p, -m, and shell expansions, you can build secure, repeatable directory structures for apps and websites. If you host with YouStable, our engineers can help you standardize directory layouts and permissions across your environments for smoother deployments and better security.

Sanjeet Chauhan

Sanjeet Chauhan is a blogger & SEO expert, dedicated to helping websites grow organically. He shares practical strategies, actionable tips, and insights to boost traffic, improve rankings, & maximize online presence.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top