Linux Mkdir Command Guide

Published August 13, 2024

This article covers the mkdir command, a basic Linux tool for creating directories. You'll learn its simple syntax, common options, and practical examples of how to use it. The article will teach you to create single directories, multiple directories, and complex nested directory structures. After reading, you'll be able to organize your files and set up project structures using mkdir.

Key Takeaways

  • Create directories easily with the mkdir command in Linux
  • Use options like -p for nested directories and -m for setting permissions
  • Create multiple directories at once by listing them or using brace expansion
  • Set up project structures and organize personal files with mkdir
  • Combine mkdir with other commands for more advanced directory management

Understanding the Mkdir Command

The mkdir command is a basic Linux utility used to create new directories.

Basic Syntax

mkdir [OPTIONS] DIRECTORY...
Component Description
OPTIONS Optional flags that modify the command's behavior
DIRECTORY Name or path of the directory to create (can specify multiple)

Common Options

Option Description
-p Create parent directories as needed
-m Set file mode (permissions) for the new directory
-v Print a message for each created directory

Example Usage

mkdir -p -v /home/user/documents/projects/project1

This command:

  • Creates the 'project1' directory
  • Creates any necessary parent directories
  • Prints a message for each directory created

Multiple Directory Creation

You can create multiple directories in a single command:

mkdir dir1 dir2 dir3

This creates three directories (dir1, dir2, dir3) in the current location.

Setting Permissions

To create a directory with specific permissions:

mkdir -m 755 new_directory

This creates 'new_directory' with read, write, and execute permissions for the owner, and read and execute permissions for others.

Verbose Output

For detailed information about directory creation:

mkdir -v project_folder

This will output a message confirming the creation of 'project_folder'.

Tip: Use Tab Completion

When creating directories with long or complex paths, use tab completion to save time and avoid typos. Start typing the path and press the Tab key to auto-complete directory names.

Creating Directories with Mkdir

Single Directory Creation

Command Description Example
mkdir <directory> Creates a directory in the current working directory mkdir project
mkdir <absolute_path> Creates a directory at the specified absolute path mkdir /home/user/documents/project

Tip: Use quotes for spaces

When creating directories with spaces in their names, use quotes to avoid errors:

mkdir "My Project Folder"

Multiple Directory Creation

Command Description Example
mkdir <dir1> <dir2> <dir3> Creates multiple directories in the current working directory mkdir dir1 dir2 dir3
mkdir <prefix>{<range>} Creates multiple directories using brace expansion mkdir project_{1..3}

Nested Directory Structure

Command Description Example
mkdir -p <path/to/nested/directory> Creates a nested directory structure, including parent directories mkdir -p parent/child/grandchild
mkdir -p <parent>/{<child1>,<child2>,<child3>} Creates multiple nested directories within a parent directory mkdir -p project/{src,docs,tests}

Advanced Mkdir Usage

Setting Directory Permissions

The mkdir command lets you set directory permissions during creation using the -m option. You can specify permissions in numeric and textual formats.

Permission Format Description
Numeric (755) Owner: rwx, Group: rx, Others: rx
Textual (u=rwx,g=rx,o=rx) User: read, write, execute; Group and Others: read, execute

Using the -m option with numeric permissions:

mkdir -m 755 my_directory

Using the -m option with textual permissions:

mkdir -m u=rwx,g=rx,o=rx my_directory

Tip: Understanding Permission Symbols

Remember that 'r' stands for read, 'w' for write, and 'x' for execute. When using textual permissions, you can mix and match these symbols to create custom permission sets for different user categories.

Verbose Mode

The -v option enables verbose output, which helps confirm directory creation and troubleshoot issues.

To enable verbose mode:

mkdir -v new_directory

Combining verbose mode with other options:

mkdir -pv parent/child/grandchild
Option Combination Description
-pv Creates parent directories as needed and provides verbose output
-mv Sets permissions and provides verbose output

Practical Examples

Creating Project Directories

When starting a new project, you can use mkdir to set up a project structure. Here's an example of creating a basic project layout:

mkdir -p my_project/{src,tests,docs,build}
mkdir -p my_project/src/{main,utils}
mkdir -p my_project/tests/{unit,integration}

This creates the following structure:

graph TD A[my_project] B[src] C[tests] D[docs] E[build] F[main] G[utils] H[unit] I[integration] A --> B A --> C A --> D A --> E B --> F B --> G C --> H C --> I

You can change this structure based on your project needs. For example, for a web development project:

mkdir -p web_project/{frontend,backend,database,config}
mkdir -p web_project/frontend/{css,js,images}
mkdir -p web_project/backend/{api,models,controllers}

Tip: Use Version Control Directories

When setting up a project structure, consider adding a directory for version control. For example:

mkdir -p my_project/.git

This creates a hidden directory for Git, which is useful if you plan to use version control for your project.

Organizing Personal Files

mkdir is also useful for organizing personal files. Here are some examples:

Creating a directory structure for photos:

mkdir -p Photos/{2023,2022,2021}/{Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}

Setting up a home directory structure:

mkdir -p ~/{Documents,Downloads,Pictures,Music,Videos}

Organizing a digital library:

mkdir -p Library/{Fiction,Non-Fiction}/{Authors,Genres}

Creating a backup structure:

mkdir -p Backups/$(date +%Y-%m-%d)/{Documents,Photos,Projects}

This creates a dated backup directory with subdirectories for different types of files.

Setting up a financial organization system:

mkdir -p Finances/{Income,Expenses,Investments,Taxes}/$(date +%Y)

For more information on using mkdir, you can refer to the GNU Coreutils manual.