mkdir Command in Linux

Learn via video courses
Topics Covered

Overview

The mkdir command in Linux is used to create new directories. It stands for 'make directory' and is a fundamental command for managing the file system hierarchy in Linux.

Linux mkdir Command Syntax

The syntax for the mkdir command is as follows:

Where:

  • OPTIONS: These are the flags that can be used to modify the behavior of the mkdir command.
  • DIRECTORY: The name of the new directory or directories to be created.

mkdir Command Options:

  • -m, --mode=MODE: Sets the file mode (permissions) for the new directory.
  • -p, --parents: Creates parent directories as needed. If the directory already exists, it does not return an error.
  • -v, --verbose: Provides more detailed output of the command execution.
  • --help: Displays help information for the command.

Example Usages

  • Create a single directory.:

    Explanation: This command creates a new directory named 'test_directory' in the current working directory.

  • Create multiple directories.:

    Explanation: This command creates three new directories (dir1, dir2, and dir3) in the current working directory.

Tips

  • Always check if a directory with the same name already exists before creating a new one, to avoid overwriting or errors.

  • Use the -p option to create a complete directory hierarchy in one command.

Advanced Use Cases of mkdir Command in Linux

  • Create a directory with specific permissions.:

    Explanation: This command creates a new directory named 'private_directory' with permissions set to 700 (rwx------), meaning only the owner has read, write, and execute permissions.

  • Create a directory hierarchy.:

    Explanation: This command creates a directory hierarchy with dir1 as the parent, dir2 as a subdirectory of dir1, and dir3 as a subdirectory of dir2. If any of the directories already exist, no error is returned.

  • Create a directory and display verbose output.:

    Output:

    Explanation: This command creates a new directory named 'verbose_directory' and displays a detailed output of the command execution.

Conclusion

  • The mkdir command is used to create new directories in Linux.

  • Various options are available to modify the behavior of the command, such as setting permissions or creating parent directories.

  • Always check for existing directories before creating new ones to avoid overwriting or errors.