Skip to content

Mkdir command linux directory creation

Command definition

The mkdir command in Linux is used to create new directories. It allows users to organize files and folders in a hierarchical structure, making it easier to manage and locate data. The command can create single or multiple directories and can also set permissions for the new directories.

Usage examples

  1. Create a single directory: This command creates a new directory named myfolder.
    bash mkdir myfolder

  2. Create multiple directories: This command creates three directories named folder1, folder2, and folder3 in one go.
    bash mkdir folder1 folder2 folder3

  3. Create a nested directory: This command creates a directory structure where myfolder contains a subdirectory named subfolder.
    bash mkdir -p myfolder/subfolder

  4. Set permissions while creating a directory: This command creates a directory with specific permissions (e.g., 755).
    bash mkdir -m 755 myfolder

  5. Create a directory with a specific owner: This command creates a directory and sets the owner to a specified user (requires superuser privileges).
    bash sudo mkdir myfolder

  6. Create directories with spaces in the name: This command creates a directory with a name that includes spaces.
    bash mkdir "my folder"

  7. Create directories using a variable: This command creates a directory using a shell variable for the directory name.
    bash dir_name=myfolder; mkdir $dir_name

  8. Create multiple nested directories: This command creates multiple nested directories in one command.
    bash mkdir -p myfolder/subfolder1/subfolder2

  9. Check if a directory exists before creating: This command checks if myfolder exists and creates it only if it does not.
    bash [ ! -d myfolder ] && mkdir myfolder

  10. Create a directory and change to it: This command creates a new directory and immediately changes the shell to that directory.
    bash mkdir myfolder && cd myfolder