[Linux] Creating New Directories with the mkdir Command

目次

Overview

mkdir (make directory) is the fundamental command used to create new directories (folders) in Linux.

Beyond simply creating a single folder, it allows you to generate deep hierarchies in a single step and set access permissions (modes) at the moment of creation. Compared to GUI folder creation, it offers overwhelming efficiency when automating tasks via scripts or generating large numbers of directories at once.


Specifications (Arguments and Options)

Syntax

mkdir [options] [directory_name...]

Main Options

OptionDescription
-pAutomatically creates any missing parent directories in the specified path (parents). Frequently used to avoid “No such file or directory” errors.
-m [mode]Sets the directory permissions (e.g., 755, 700) numerically at the time of creation (mode).
-vDisplays a message for every directory created (verbose).

Basic Usage

Creating a Directory

The simplest usage is providing the name of the directory you wish to create.

mkdir workspace

Execution Result Example:

(No output is shown if successful. Verify using the ls command)

ls -F
workspace/

Practical Commands

1. Creating Deep Hierarchies at Once (-p)

Normally, running mkdir dir1/dir2/dir3 results in an error if dir1 or dir2 do not already exist. Using the -p option creates the entire parent hierarchy recursively.

# Automatically creates the 'logs' and 'app' directories if they don't exist
mkdir -p logs/app/2026

2. Creating Multiple Subdirectories (Brace Expansion)

This is useful when you need to set up a standard project structure. It utilizes the shell’s brace expansion {A,B,C} feature.

# Creates src, bin, and doc directories inside the 'project' folder simultaneously
mkdir -p project/{src,bin,doc}

Execution Result Example:

tree project
project
├── bin
├── doc
└── src

3. Creating a Private Directory (-m)

When creating a directory for sensitive information, you can skip the extra chmod step by setting permissions immediately.

# Create a directory accessible only by the owner (700)
mkdir -m 700 private_data

Execution Result Example:

ls -ld private_data
drwx------ 2 user user 4096 Jan 16 10:00 private_data

Note: For directories, the execution (x) permission is required to enter the folder. Therefore, 700 is standard rather than 400.


Customization Points

  • Error Suppression with -p: The -p option has the side effect of not throwing an error if the directory already exists. This makes it ideal for scripts that ensure a directory “is there.”
  • Permission Settings (-m): Use -m 755 for public web content and -m 700 for strictly private data.

Important Notes

  • Directory Permissions: Unlike files, if a directory lacks the execution (x) permission, you cannot cd into it or list its contents with ls. Creating a folder with -m 400 results in a locked folder that even the owner cannot enter without a chmod fix.
  • Write Permissions: To create a directory, you must have write (w) permissions in the parent directory. Creating folders in system paths like /usr/local/ requires sudo mkdir.

Advanced Application

Creating a Backup Directory with the Current Date

Use command substitution to create a folder named with the current date (YYYY-MM-DD format).

mkdir -p backup/$(date +%F)

Conclusion

mkdir is a simple command, but the -p option significantly changes your workflow efficiency.

Reminder: Always ensure the owner has execution (x) permissions for the directory.Using mkdir -p is the standard “best practice” in the field to avoid errors and ensure reliability.

Ideal Use Cases: General folder creation and hierarchical setup in scripts.

Key Options: Use -p for hierarchies and -m for specific permissions.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次