Skip to main content

Understanding the `mkdir` Command in Linux

Linux offers a multitude of commands to facilitate various operations for users, one of which is mkdir — a shorthand for "Make Directory." This command-line utility allows users to create new directories (or folders) in the file system. In this article, we will dive deep into the mkdir command, exploring its syntax, options, and examples.

Basic Syntax

The basic syntax of the mkdir command is as follows:

mkdir [OPTIONS] DIRECTORY_NAMES
  • OPTIONS are the optional flags that modify the behavior of the mkdir command.
  • DIRECTORY_NAMES specify the name(s) of the directory or directories to be created.

Basic Usage

Creating a new directory is simple. Open a terminal window and type:

mkdir new_directory

This will create a directory named new_directory in the current working directory.

Options

OptionShorthandDescription
--parents-pCreates parent directories as needed. If the specified directories exist, no error is returned.
--mode-mSets the permission mode for the newly created directories. The mode is specified in octal numbers.
--verbose-vPrints a message to the terminal for each directory that is created, useful for debugging.
--helpN/ADisplays a help message detailing the options and usage of the mkdir command.
--versionN/AOutputs the version information of the mkdir command currently installed on the system.

The mkdir command offers several options that can be used to customize the behavior of the command.

-p, --parents

This option allows you to create parent directories as needed. If you specify a path that includes non-existent directories, this flag will create those parent directories for you.

mkdir -p dir1/dir2/dir3

In the above example, if dir1 and dir2 don't exist, mkdir will create them before creating dir3.

-m, --mode

Sets the permission mode for the new directory. It's expressed in octal numbers.

mkdir -m 700 private_directory

Here, only the owner of private_directory will have read, write, and execute permissions.

--verbose

When this option is used, mkdir will print a message for each directory that it creates.

mkdir --verbose new_directory

Output:

mkdir: created directory 'new_directory'

-v

This is the shorthand for --verbose.

mkdir -v another_directory

--help

Displays the help menu for the mkdir command.

mkdir --help

--version

Displays the version information of the mkdir command.

mkdir --version

Combining Options

You can combine multiple options to perform complex tasks.

mkdir -pv -m 755 dir1/dir2/dir3

In this example, -p ensures that parent directories are created if they don't exist, -v makes the operation verbose, and -m 755 sets read, write, and execute permissions for the owner, and read and execute permissions for the group and others.

Examples

Here are some practical examples:

  1. Creating a single directory

    mkdir single_dir
  2. Creating multiple directories

    mkdir dir1 dir2 dir3
  3. Creating nested directories

    mkdir -p parent/child/grandchild
  4. Creating a directory with specific permissions

    mkdir -m 700 secure_dir
  5. Creating multiple directories with verbose output

    mkdir -v dir1 dir2 dir3

Conclusion

The mkdir command in Linux is a powerful tool for creating directories. It comes with various options that allow you to create multiple directories, set permissions, and even create parent directories as needed. Once you understand how to use mkdir and its options effectively, managing your file system becomes a much more streamlined process.

TypeScript Course Instructor Image
TypeScript Course Instructor Image

Time To Transition From JavaScript To TypeScript

Level Up Your TypeScript And Object Oriented Programming Skills. The only complete TypeScript course on the marketplace you building TypeScript apps like a PRO.

SEE COURSE DETAILS

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for ssoftware developers.

YouTube @cloudaffle