Skip to content

Linux Foundations / Find your way around Linux ยท Step 23 of 63

Lesson: mkdir (Make Directory)Switch
โ† Linux Foundations

Shell and files

mkdir (Make Directory)

Create single and nested directories with mkdir and set their permissions.

mkdir makes new directories. Directories keep related files together, and you will create them constantly.

Making a directory

$ mkdir projects
$ ls -F
projects/

Success is silent. ls -F adds a / after directory names.

You can create several at once:

$ mkdir docs images music

Each argument is a separate directory, so quote a name containing spaces: mkdir "Holiday Photos".

When it fails

If the name is already taken, you get an error:

$ mkdir projects
mkdir: cannot create directory โ€˜projectsโ€™: File exists

It also fails if a parent directory is missing:

$ mkdir work/2026/june
mkdir: cannot create directory โ€˜work/2026/juneโ€™: No such file or directory

Creating parents with -p

-p creates any missing parents along the way, and does not complain if the directory already exists:

$ mkdir -p work/2026/june

This makes work, then 2026, then june. It is very handy in scripts, which may run more than once. It still fails if an ordinary file is in the way.

-v reports each directory as it is created:

$ mkdir -pv a/b
mkdir: created directory 'a'
mkdir: created directory 'a/b'

Permissions

New directories get default permissions that are limited by your umask, often giving drwxr-xr-x or drwxrwxr-x. Use -m to set the mode of the new directory directly:

$ mkdir -m 700 private
$ ls -ld private
drwx------ 2 pete pete 4096 Nov 29 11:00 private

Mode 700 lets only the owner read, write and enter it. With -p, -m applies to the final directory, not to the parents it creates.

Brace expansion

Bash can expand one word into several before mkdir sees it:

$ mkdir -p site/{css,js,img}
$ ls site
css  img  js

Do not put spaces inside the braces. This is a shell feature, so it may not work in a basic sh.

You can practise in the files and links lab.

Try it

  1. Create a directory called practice.
  2. Inside it, make a/b/c with one command.
  3. Create a private directory with mode 700.
  4. Use braces to create three sibling directories.

Check your understanding

mkdir (Make Directory)

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.