Skip to main content

The mkdir Command

The mkdir command is used to create directories, which are the building blocks of the file system hierarchy. Just as folders help organize files on your desktop, directories serve to organize and manage files within the operating system. This tutorial will provide you with a thorough understanding of mkdir and its capabilities.

Introduction to mkdir

The mkdir command stands for "make directory" and is used to create new directories within the file system. It's a command-line utility, which means you'll be using it within a terminal emulator or console. The simplicity and versatility of mkdir make it an indispensable command for all users, from system administrators to casual users.

Basic Syntax

The basic syntax for the mkdir command is as follows:

mkdir [options] directory_name

Here, directory_name is the name of the directory you wish to create. The [options] part is where you can specify various flags to alter the behavior of mkdir.

Creating a Single Directory

To create a single directory, you simply type mkdir followed by the name of the directory you want to create. For example:

mkdir my_new_directory

This command will create a new directory named my_new_directory in the current working directory.

Creating Multiple Directories

You can also create multiple directories with a single mkdir command by listing the directory names separated by spaces:

mkdir dir1 dir2 dir3

This will create three new directories: dir1, dir2, and dir3.

Using Absolute and Relative Paths

mkdir can be used with both absolute and relative paths. An absolute path specifies the location of the directory relative to the root of the file system, while a relative path specifies the location relative to the current working directory.

For example, to create a directory in your home directory using an absolute path:

mkdir /home/username/new_directory

And to create the same directory using a relative path (assuming you're currently in your home directory):

mkdir new_directory

Understanding Parent Directories

When creating a directory with mkdir, the command will not create any missing parent directories by default. If you try to create a directory within a non-existent parent directory, mkdir will return an error:

mkdir: cannot create directory ‘parent_dir/child_dir’: No such file or directory

To resolve this, you can either manually create the parent directory first or use the -p option, which tells mkdir to create the parent directories as needed:

mkdir -p parent_dir/child_dir

This command will create both parent_dir and child_dir if they do not already exist.

Interactive Mode with -i

The -i option prompts the user before creating a directory that already exists, which can prevent accidental overwrites:

mkdir -i existing_directory

If existing_directory already exists, mkdir will ask for confirmation before proceeding.

Setting Permissions with -m

The -m option allows you to set the permissions of the newly created directory. For example:

mkdir -m 755 new_directory

This command creates new_directory with read, write, and execute permissions for the owner, and read and execute permissions for the group and others.

Verbose Output with -v

The -v option provides verbose output, displaying a message for each directory created:

mkdir -v dir1 dir2

This will output something like:

mkdir: created directory 'dir1'
mkdir: created directory 'dir2'

Watch Out For!

  • Ensure you have the necessary permissions to create directories in the target location.
  • Be cautious with the -i option to avoid overwriting existing directories.
  • When using -m to set permissions, make sure you understand the implications of the permissions you're setting for security reasons.

With the various options and examples provided in this guide, you should now be equipped to create directories efficiently and effectively.