The mkdir command, short for make directory, creates directories for organizing files and other directories.
Command Line · Lesson 12
mkdir (Make Directory)
Learn how to create single, multiple, and nested directories with mkdir options.
The basic syntax is:
mkdir [OPTIONS] DIRECTORY...
Creating One Directory
Pass a pathname to create one directory. This example creates documents in the current working directory:
$ mkdir documents
If an entry named documents already exists, mkdir reports an error rather than replacing it. Use ls -ld documents to inspect the existing entry.
Which command creates a directory named documents in the current working directory?
Creating Multiple Directories
List several pathnames to create several directories in one command:
$ mkdir books paintings
Which command creates two sibling directories named books and paintings?
Creating Missing Parent Directories
Without an option, mkdir books/hemingway/favorites fails if an intermediate directory is missing. Add -p to create missing parent directories along the path:
$ mkdir -p books/hemingway/favorites
This creates the missing parts of the path. It also does not report an error merely because the final directory already exists, although other errors such as insufficient permissions can still occur.
None of projects/app/src exists yet. Which command creates the complete directory path?
Setting the Initial Mode
Use -m MODE to specify permissions for a newly created directory:
$ mkdir -m 755 public
You will study permission modes later. In this example, mode 755 gives the owner read, write, and search permissions, while the group and others receive read and search permissions.
Add -v to print a message for each directory as it is created:
$ mkdir -pv projects/app/src
mkdir: created directory 'projects'
mkdir: created directory 'projects/app'
mkdir: created directory 'projects/app/src'
Which command creates public with permission mode 755?
To practice creating and organizing directories, try these hands-on labs:
- Linux mkdir Command: Directory Creating - Learn how to use the
mkdircommand in Linux to create directories, set permissions, and organize your file system. This lab covers basic and advanced usage, including creating nested directories. - Setting Up a New Project Structure - Practice your Linux directory management skills by creating a specific project structure and navigating through it using essential commands like
mkdirandcd.
Lesson complete
You finished mkdir (Make Directory)
You can now create directory structures with deliberate names, parents, and modes.
Create one or more directories in a single command.
Recognize an error caused by an existing pathname.
Build missing parent directories with
-p.Set a new directory's mode with
-m.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account