mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a new directory with the specified name. The basic syntax of the "mkdir" command is:

mkdir [options] directory_name

When executed without any options, the "mkdir" command will create a new directory with the specified name in the current working directory. For example, the following command will create a new directory named "example":

mkdir example

The "mkdir" command also has several options that can be used to control how the new directory is created. The most commonly used options are:

-p : creates any necessary parent directories along the path specified. For example, the following command will create a new directory named "example" and any necessary parent directories along the path:

mkdir -p /path/to/example

-m : sets the file mode (permissions) for the newly created directory. For example, the following command will create a new directory named "example" with permissions set to 755:

mkdir -m 755 example

-v : displays verbose output, showing each directory as it is created. For example, the following command will create a new directory named "example" and display verbose output:

mkdir -v example

-Z : sets the SELinux context for the newly created directory. This option is used on systems with SELinux enabled. For example, the following command will create a new directory named "example" and set the SELinux context to "user_u:object_r:var_t":

mkdir -Z user_u:object_r:var_t example

The "mkdir" command can also be used in shell scripts and automation tasks to create new directories as needed. For example, a script may use the "mkdir" command to create a new directory to store backups, or to create a new directory for a new user account.

The "mkdir" command is a fundamental command for creating directories in Linux and other Unix-like operating systems. Here are some additional features and uses of the "mkdir" command:

  • The "mkdir" command can also be used to create multiple directories at once. You can specify multiple directory names as arguments to the command and it will create all of them. For example, the following command will create the directories "example1" and "example2" in the current working directory:
    • mkdir example1 example2
  • The "mkdir" command can also be used in combination with other commands to create directories dynamically. For example, the following command will create a new directory with the name "backup-<current-date>" in the current working directory, where <current-date> is the current date in the format YYYY-MM-DD:
    • mkdir backup-$(date +%F)
  • The "mkdir" command can also be used in shell scripts and automation tasks to create directories with specific naming conventions. For example, a script may use the "mkdir" command to create new directories with names in the format "backup-<current-date>" to store backups, or to create new directories with names in the format "user-<username>" to store files for specific users.
  • The "mkdir" command also provides the ability to set the ownership and group of the new directory by using the option -o, and -g. This can be useful when running scripts as a non-root user and creating directories with specific permissions.

In summary, the "mkdir" command is a powerful command that can be used to create directories in Linux and other Unix-like operating systems. It can be used to create multiple directories at once, create directories dynamically, and create directories with specific naming conventions. It can also be used in combination with other commands and in shell scripts and automation tasks. It also provides the ability to set ownership and group of the directory, which can be useful when running scripts as a non-root user and creating directories with specific permissions.