keepcalculator

06 Numbers and Powers

Mkdir Command Examples & Usage

Card No. 06·48580 · Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered
You enter
Finish time, Distance
You get
Average pace, Average speed
Rounding
2 dp

The mkdir command creates a new directory (folder) in Linux, macOS, and other Unix-like systems. You provide a directory name, and it creates that directory in your current location unless you specify a path. This article shows the most common mkdir examples, explains its options, and covers common pitfalls.

What Is mkdir?

mkdir is a standard command-line utility present in all Unix-like operating systems, including Linux distributions, macOS, and the Windows Subsystem for Linux (WSL). Its name stands for “make directory.” The command is part of the POSIX standard, so its core behavior is consistent across systems. When you run mkdir, it creates one or more empty directories. If you want to create a directory with a non-empty structure, you would use additional options or other commands like touch to add files.

Basic Syntax and Common Examples

The general syntax is:

mkdir [options] directory_name

Here are the most frequently used examples:

  • Create a single directory: mkdir projects – creates a directory named projects in the current working directory.
  • Create multiple directories at once: mkdir docs images videos – creates three separate directories in one command.
  • Create a directory with spaces in its name: mkdir "my documents" – the quotes are required so the shell treats the space as part of the name.
  • Create a directory with a path: mkdir /home/user/newfolder – creates the directory at the specified absolute path, provided the parent directory exists.
  • Create a directory in a relative location: mkdir ../backup – creates a directory named backup one level above the current directory.

Using the -p (Parents) Option

The -p option is one of the most useful. It creates intermediate directories as needed. For example, mkdir -p a/b/c will create a, then a/b, then a/b/c – all in one go. Without -p, the command fails if any parent directory is missing. This option also suppresses the error if the directory already exists, which is handy in scripts. For instance, mkdir -p reports/2025 will succeed whether reports exists or not.

Setting Permissions with -m

You can set permissions on the new directory using the -m option. Permissions are expressed in octal notation (e.g., 755, 700, 644). For example, mkdir -m 700 private creates a directory with read, write, and execute permissions for the owner only, and no permissions for group or others. This is useful for sensitive data. Without -m, the directory is created with the default permissions based on your system’s umask (usually 755 for directories).

Verbose Output with -v

The -v (verbose) option makes mkdir print a message for each directory it creates. Example: mkdir -v newdir outputs mkdir: created directory 'newdir'. This is helpful in scripts or when you want confirmation that the command did what you expected, especially when creating multiple directories.

Common Mistakes and How to Avoid Them

  • Forgetting quotes for spaces: mkdir my folder creates two directories, my and folder. Always quote names with spaces.
  • Missing parent directories: mkdir a/b/c fails if a/b doesn’t exist. Use -p to create the whole path.
  • Using a leading slash accidentally: mkdir /newdir tries to create a directory at the root of the filesystem, not in the current directory. If you meant a relative path, omit the slash.
  • Case sensitivity: On Linux and macOS, Data and data are different names. Be consistent.
  • Typing the command as mrdir: A common typo. Double-check your spelling.

Alternatives and Related Commands

While mkdir is the standard, there are other ways to create directories:

  • Using install: install -d newdir creates a directory (and can also set ownership and permissions).
  • Using a file manager: GUI file managers (like Nautilus, Finder, or File Explorer) have a “New Folder” button, which is easier for beginners.
  • Using touch with a trailing slash: touch newdir/ actually fails because touch expects a file, not a directory. So don’t use that.
  • In Windows Command Prompt (cmd): The equivalent is md or mkdir. In PowerShell, New-Item -ItemType Directory -Path newdir is used.

Frequently Asked Questions

Q: How do I create a directory with a space in the name?
A: Enclose the name in quotes: mkdir "my folder". Alternatively, escape the space with a backslash: mkdir my\ folder.

Q: What does mkdir -p do exactly?
A: It creates all intermediate directories in a path. For example, mkdir -p a/b/c creates a, a/b, and a/b/c if they don’t exist. It also does not error if the directory already exists.

Q: How do I create a directory with specific permissions?
A: Use the -m option with an octal permission code. For example, mkdir -m 700 secret creates a directory readable/writable/executable only by the owner.

Q: Why does mkdir fail with “No such file or directory”?
A: This usually means a parent directory in the path does not exist. Use -p to create the entire path, or ensure the parent exists first.

Q: Can I create multiple directories at once?
A: Yes, simply list them: mkdir one two three. You can also use brace expansion in bash: mkdir {one,two,three}.

Q: Is mkdir available on Windows?
A: Yes, in Command Prompt and PowerShell, mkdir works. In PowerShell, you can also use New-Item -ItemType Directory.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0 | 100000
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →