Setting Permissions with mkdir Command in Linux: Creating Directories and Assigning Permissions in a Single Command

Nov 21, 2025 · Programming · 14 views · 7.8

Keywords: Linux | mkdir command | permission settings | directory creation | file system

Abstract: This article provides an in-depth exploration of using the mkdir command in Linux systems to create directories while directly setting permissions through the -m option, achieving directory creation and permission assignment in a single command. It details the syntax structure of the mkdir command, the principles of permission mode settings, and demonstrates applications in various permission scenarios through multiple practical code examples. Advanced usage such as creating multi-level directories and batch directory creation is also covered to enhance efficiency for system administrators and developers.

Basic Syntax and Permission Settings of the mkdir Command

In Linux systems, the mkdir command is the core tool for creating directories. Its basic syntax is mkdir [options] directory_name, where the -m option is specifically used to set permission modes directly during directory creation.

Implementing Directory Creation and Permission Setting in a Single Command

According to the best answer in the Q&A data, using the command mkdir -m 777 dirname accomplishes directory creation and permission assignment in one step. Here, the -m option is followed by the permission mode value, with 777 granting read, write, and execute permissions to all users.

Detailed Explanation of Permission Modes

Permission modes in Linux systems use octal notation:

Each digit ranges from 0 to 7, where:

4 = read permission (r)
2 = write permission (w)
1 = execute permission (x)

Thus, the 777 permission corresponds to the binary representation 111 111 111, meaning all users have full read, write, and execute permissions.

Practical Application Examples

The following code demonstrates practical applications of different permission settings:

Basic Permission Settings

# Create directory and set 777 permissions
mkdir -m 777 public_dir

# Create directory and set 755 permissions (full for owner, read and execute for others)
mkdir -m 755 secure_dir

# Create directory and set 700 permissions (full permissions for owner only)
mkdir -m 700 private_dir

Creating with Paths

# Create directory at specified path with permissions
mkdir -m 777 /var/www/uploads

# Using relative path
mkdir -m 755 ./config/cache

Advanced Usage and Option Combinations

Creating Multi-level Directory Structures

Combining with the -p option allows creation of multi-level directory structures:

# Create multi-level directories and set permissions
mkdir -p -m 755 project/src/main/java

This command creates the full directory path, automatically creating any intermediate directories if they do not exist, and sets 755 permissions for the final directory.

Batch Directory Creation

Using brace expansion to create multiple directories in batch:

# Batch create multiple directories with same permissions
mkdir -m 755 {dir1,dir2,dir3}

# Create sequential directories
mkdir -m 777 user{1..10}

Permission Verification and Debugging

After creating directories, use the following commands to verify permission settings:

# View directory permissions
ls -ld directory_name

# List detailed directory information
stat directory_name

Security Considerations

Although 777 permissions offer maximum access flexibility, they should be used cautiously in production environments:

Comparison with Other Commands

Compared to the traditional two-step approach, the single-command method offers significant advantages:

# Traditional approach (two steps)
mkdir mydir
chmod 777 mydir

# Optimized approach (single step)
mkdir -m 777 mydir

The single-command method not only reduces the number of commands but also avoids intermediate states, enhancing atomicity and reliability of operations.

Conclusion

The mkdir -m command combination provides an efficient way to manage file system operations in Linux. By appropriately using permission modes and option combinations, precise directory permission control can be achieved while maintaining code simplicity and maintainability. In practical applications, it is recommended to select appropriate permission settings based on specific scenarios, balancing functional requirements with security considerations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.