LinuxCommandLibrary

mkdir

Create new directories

TLDR

Create specific directories

$ mkdir [path/to/directory1 path/to/directory2 ...]
copy

Create specific directories and their parents if needed
$ mkdir [[-p|--parents]] [path/to/directory1 path/to/directory2 ...]
copy

Create directories with specific permissions
$ mkdir [[-m|--mode]] [rwxrw-r--] [path/to/directory1 path/to/directory2 ...]
copy

Create multiple nested directories recursively
$ mkdir [[-p|--parents]] [path/to/{a,b}/{x,y,z}/{h,i,j]}
copy

SYNOPSIS

mkdir [OPTION]... DIRECTORY...

PARAMETERS

-m MODE
--mode=MODE

    Sets the file mode (permissions) for the created directory. MODE can be an octal number (e.g., 755 for rwxr-xr-x) or a symbolic representation (e.g., u=rwx,go=rx).

-p
--parents

    Creates parent directories as needed. No error is reported if a directory already exists. This is particularly useful for creating deep, nested directory structures with a single command.

-v
--verbose

    Prints a message for each directory created, providing feedback on the command's execution.

--context=CTX
    Sets the SELinux security context of the created directory to CTX. Only available on systems with SELinux enabled.

--help
    Displays a help message with command usage and options, then exits.

--version
    Outputs version information for the mkdir command and then exits.

DESCRIPTION

mkdir is a fundamental command-line utility used to create new directories (folders) in the file system. It allows users to organize files by creating structured paths for storing data.

When invoked, mkdir creates a new directory at the specified path. By default, it creates only the last component of the path, failing if any parent directory in the path does not exist. However, the -p (parents) option allows mkdir to create any necessary parent directories along the way if they don't already exist.

Directories are created with default permissions, typically rwxrwxrwx (0777) modified by the user's umask. The -m (mode) option provides explicit control over these permissions. mkdir is crucial for managing and structuring data efficiently within the Linux environment, enabling users to maintain an organized file hierarchy. It can create multiple directories simultaneously by listing them as arguments.

CAVEATS

When creating directories, the default permissions are significantly affected by the user's umask setting, which can restrict permissions (e.g., preventing group or others from writing).

Without the -p option, mkdir will fail if any parent directory in the specified path does not exist. It also fails if the target directory name already exists at the specified location (unless -p is used, in which case it does nothing).

Users must have appropriate write permissions in the parent directory where the new directory is to be created. Filesystem path length limits, which vary by operating system, may also apply.

UNDERSTANDING UMASK

The umask (user file-creation mask) command determines the default file and directory permissions when they are created. For directories, the default permission is initially 0777 (rwxrwxrwx). The umask value is then subtracted from this initial value to determine the final permissions. For example, if your umask is 0022, a new directory will be created with permissions 0755 (0777 - 0022 = 0755), meaning owner has full permissions, while group and others can only read and traverse.

Using mkdir -m allows you to explicitly set permissions for a specific directory, overriding the effect of the umask.

PERMISSIONS MODES

Directory permissions control who can read its contents, write (create/delete files/subdirectories) within it, and execute/traverse into it. They are commonly specified in octal notation (e.g., 755), where each digit represents permissions for the owner, group, and others, respectively.

The 'execute' permission (x or 1 in octal) for a directory is crucial: it allows a user to enter or traverse into the directory and access its contents, even if they don't have read permission on the directory itself (though they couldn't list its contents in that case).

HISTORY

The mkdir command has been a fundamental utility since the early days of Unix. It was included in the first versions of Unix in the 1970s and has since been standardized by POSIX (Portable Operating System Interface). Its core functionality has remained remarkably consistent over decades, reflecting its essential and unchanging role in basic file system management. It is implemented as a system call in the kernel, making it a very low-level and efficient operation.

SEE ALSO

rmdir(1), rm(1), ls(1), cd(1), chmod(1), touch(1)

Copied to clipboard