Linux mkdir Command: Directory Creating

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial introduces the mkdir command in Linux, focusing on its role in creating directories within the file system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") subgraph Lab Skills linux/mkdir -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} linux/ls -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} shell/comments -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} shell/quoting -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} end

mkdir Command

The mkdir command, short for "make directory" is used to create new directories or folders in a Linux terminal. It is a fundamental tool for organizing the file structure.

Command Usage

Let's start with a simple example. To create a directory named new_folder in the current working directory, run the following mkdir command and ls command to display the current working files and directories:

terminal

Input:

mkdir /home/labex/project/new_folder
ls /home/labex/project

Output:

new_folder ## new folder has been created.

In this step, we'll cover the essential usage of the mkdir command without exploring optional parameters or advanced features.

Parameters and Usage Examples

This tutorial extends the knowledge of the mkdir command by introducing various options that can be used to customize its behavior.

Option Parameter

mkdir [options] directory

  • -p: It can be a path name. If some of the path's directories do not exist, this option will automatically create directories that do not exist. You may create multiple directories at a time.
  • -m: Set permissions of the newly created directory.
  • -v: Display detailed information while creating directories.

Example Usage

1. Create Directory with Parent Directories (-p)

The -p option with mkdir allows the creation of a directory and its parent directories if they do not exist. This is useful to create a nested directory structure without worrying about whether the parent directories are already present.

Input:

mkdir -p /home/labex/project/new_dir/sub_dir
ls /home/labex/project/new_dir

Output:

sub_dir

2. Set Permissions (-m)

The -m option with mkdir allows you to specify the permissions of the newly created directory explicitly. You can use octal notation to define the permissions.

Input:

mkdir -m 755 /home/labex/project/permission_dir
ls -ld /home/labex/project/permission_dir

Output:

drwxr-xr-x 2 user group 4096 Dec 5 00:00 /home/labex/project/permission_dir

3. Verbose Mode (-v)

The -v option with mkdir enables verbose mode, which displays detailed information about the directories being created.

Input:

mkdir -v /home/labex/project/verbose_dir

Output:

mkdir: created directory '/home/labex/project/verbose_dir'

Summary

In summary, the mkdir command is a versatile tool for creating directories in Linux. Whether you need to create nested directories, set specific permissions, or receive detailed feedback on the creation process, mkdir provides essential functionality for file system organization.

Other Linux Tutorials you may like