Creating directories: mkdir

Objectives
  • Create new directories from the command line, both inside and outside the current directory.
  • Demonstrate how to create directories with whitespaces in their name.
Prerequisites
  • None.

Creating directories

Use mkdir to create a new directory.

TIP:

The -v (verbose) option instructs mkdir to give more information about what it has done.
 

Example: creating a directory inside your current directory

sarah@laptop ~/photos$ mkdir spain2022
sarah@laptop ~/photos$

→ The above command creates a directory named spain2022 in the current directory.

→ Use ls to list the contents of the current directory. The new directory will be listed.

Example: creating a directory outside your current directory

sarah@laptop ~/photos$ mkdir ../documents/homework
sarah@laptop ~/photos$

→ The above command creates the new directory inside ~/photos/documents

Directories with spaces in their name

There are two ways to work with directories that have spaces in their name:

Use single or double quotes

sarah@laptop:~/Documents$ mkdir "Updated reports"

Use backslash to escape each whitespace character

sarah@laptop:~/Documents$ mkdir Updated\ reports

Both methods are equivalent; feel free to use the one you are most comfortable with.

Activity

Create a new directory and use the -v option to see verbose output.

mkdir -v my-new-directory

Hide hints
show hints

Summary

  • Use mkdir to create a new directory.
  • If the directory name contains whitespaces, wrap the name in quotes or escape each whitespace character with a backslash (\)