Introduction
In this tutorial, you'll discover how to expertly create and manage directories using the Linux mkdir
command. This guide covers creating individual and multiple directories, generating nested directory structures using the -p
option, and effectively managing directory permissions with mkdir
. Expect practical examples and detailed, step-by-step instructions designed to empower you in performing fundamental file and directory operations within a Linux environment, essential for any systemadmin.
Create Directories with mkdir Command
This section demonstrates how to create directories using the mkdir
command within a Linux system.
The mkdir
command serves the purpose of generating new directories. You have the flexibility to create a single directory or several directories simultaneously.
The basic syntax for creating a directory is:
mkdir directory_name
Example:
$ mkdir mydir
$ ls
mydir
In this instance, we've generated a directory labeled mydir
employing the mkdir
command. The creation of the directory can be confirmed via the ls
command.
To create multiple directories at once, simply list their names, separated by spaces:
mkdir dir1 dir2 dir3
Example output:
$ mkdir dir1 dir2 dir3
$ ls
dir1 dir2 dir3 mydir
Now, let's create a directory structure with multiple levels:
mkdir -p parent/child/grandchild
The -p
option used with mkdir
allows the creation of an entire directory tree, including any required parent directories, with a single command.
Example output:
$ mkdir -p parent/child/grandchild
$ ls -R
.:
child parent
./parent:
child
./parent/child:
grandchild
As demonstrated, mkdir -p
created the parent
, child
, and grandchild
directories in one go.
Create Nested Directories with mkdir -p
This section details how to create nested directories leveraging the mkdir -p
command in Linux.
The mkdir -p
command enables the creation of a multi-level directory structure with a single command. This is particularly useful when you need to create a directory along with its parent directories.
Consider the following nested directory creation:
mkdir -p projects/web-app/src/components
Example output:
$ mkdir -p projects/web-app/src/components
$ ls -R
projects
./projects:
web-app
./projects/web-app:
src
./projects/web-app/src:
components
Here, mkdir -p
created the complete structure, including projects
, web-app
, src
, and components
.
Now, consider another nested directory structure:
mkdir -p documents/reports/2023/q1
Example output:
$ mkdir -p documents/reports/2023/q1
$ ls -R
documents projects
./documents:
reports
./documents/reports:
2023
./documents/reports/2023:
q1
./projects:
web-app
The mkdir -p
command enabled the creation of the full directory path, including documents
, reports
, 2023
, and q1
in a single command.
Permissions Management with mkdir
This segment explains how to manage directory permissions during creation using the mkdir
command within Linux, which is crucial for any systemadmin securing their servers.
By default, a new directory created with mkdir
inherits the permissions of its parent directory. However, you can set specific permissions during creation.
To create a directory with particular permissions, employ the -m
option, followed by the permission mode:
mkdir -m 755 my_dir
In this example, my_dir
is created with permissions set to 755
(read, write, and execute for the owner; read and execute for the group and others).
Symbolic permissions can also be used in place of numeric modes:
mkdir -m u=rwx,g=rx,o=rx my_dir
This command achieves the same permission settings as the preceding example for my_dir
, but utilizes symbolic notation.
Let's establish a directory with differing permissions:
mkdir -m 700 secret_dir
This creates secret_dir
with permissions set to 700
(read, write, and execute for the owner; no access for group and others), making it accessible only to the user, often the root user or systemadmin.
The permissions of directories can be verified using the ls -l
command:
$ ls -l
total 8
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 my_dir
drwx------ 2 labex labex 4096 Apr 12 12:35 secret_dir
The output shows my_dir
with permissions 755
and secret_dir
with permissions 700
.
Summary
In this lab, you've gained proficiency in creating directories using the mkdir
command in Linux. This includes creating single or multiple directories at once, and the creation of nested directories with the -p
option. Moreover, you've learned how to manage permissions while creating directories, ensuring secure and organized file systems. The critical takeaways include the varied applications of mkdir
for basic directory management, advanced nested structure creation, and precise permission control essential skills for any systemadmin.