Read

Module 2 · Files and directories

Creating directories with mkdir

Making one directory, several at once, or a whole nested path in a single command, and why -p is the option you will almost always want.

What you will learn

  • Create directories by relative and absolute path.
  • Create nested paths in one go with `-p`.
  • Read and handle `mkdir`'s two error messages.

mkdir — *make directory* — creates directories. Give it a name and it makes that directory inside your working directory; give it a path and it makes it there. Give it several names and it makes them all. It says nothing when it succeeds, like most Unix commands, so follow up with ls if you want to see the result.

~% cd lab/l11
l11% mkdir notes
l11% mkdir src docs tests
l11% ls -F
docs/  notes/  src/  tests/
l11% mkdir /root/lab/l11/notes/2026
l11% ls notes
2026

Nested paths and -p

By default mkdir creates only the last component of a path and expects everything before it to exist. mkdir archive/2024/jan fails if archive or archive/2024 is missing. The -p option (*parents*) removes that restriction: it creates every missing directory along the way, and it does not complain about the ones that already exist.

l11% mkdir archive/2024/jan
mkdir: can't create directory 'archive/2024/jan': No such file or directory
l11% mkdir -p archive/2024/jan
l11% ls -R archive
archive:
2024

archive/2024:
jan

archive/2024/jan:

The second property is as useful as the first. Because mkdir -p is quiet when the directory is already there, it is idempotent: running it twice has the same effect as running it once. That makes it safe in scripts and setup commands, where you want to guarantee a directory exists without caring whether it did before. Plain mkdir on an existing name fails with File exists.

MessageCauseFix
No such file or directoryA parent in the path does not exist.Add -p, or create the parents first.
File existsSomething — a directory *or a file* — already has that name.Pick another name, or use -p if a directory is fine.

Permissions at creation: -m

A new directory gets default permissions — here drwxr-xr-x, readable by everyone. -m MODE sets them at creation instead: mkdir -m 700 private makes a directory only its owner can enter (drwx------). The numbers are explained in lesson 47; for now, know that the option exists so you do not have to create and then chmod.

Commands in this lesson

CommandWhat it does
mkdir DIRCreate DIR; its parent must exist.
mkdir A B CCreate several directories at once.
mkdir -p A/B/CCreate the whole path, missing parents included; quiet if it exists.
mkdir -m 700 DIRCreate with specific permissions.

Quiz

  1. `mkdir a/b/c` fails with `No such file or directory`. Why?

    • `a` or `a/b` does not exist yet
    • Directory names cannot contain slashes
    • `c` already exists
  2. What does `mkdir -p docs` do if `docs` already exists?

    • Fails with `File exists`
    • Nothing, silently
    • Empties the directory
  3. How many directories does `mkdir src docs tests` create?

    • One, named `src docs tests`
    • Three
    • None; it needs `-p`
  4. `mkdir report` answers `File exists`, but `ls -F` shows `report` without a slash. What is going on?

    • A regular *file* called `report` occupies the name
    • The directory is hidden
    • `ls -F` is broken
  5. Which command creates a directory only its owner can enter?

    • `mkdir -p private`
    • `mkdir -m 700 private`
    • `mkdir --private`

Practice

  1. In /root/lab/l11, create a directory called `notes`.

  2. Create the nested path /root/lab/l11/archive/2024/jan with a single command. None of the three levels exists yet.

  3. Inside /root/lab/l11/project, create three directories — `src`, `docs` and `tests` — using one command.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.