Linux is an incredibly powerful and versatile operating system, allowing users to create, manage, and manipulate files and directories with ease. One such essential tool in a Linux user’s toolkit is the “mkdir” command, which allows you to create directories swiftly. In this blog post, we’ll dive into the mkdir command, its usage, and some practical examples to help you better understand and leverage this powerful tool.
What is the mkdir command?
The “mkdir” command is short for “make directory.” It’s a built-in command-line utility in Linux that allows users to create new directories. The basic syntax for the mkdir command is as follows:
mkdir [options] [directory_name(s)]
Here, ‘options’ are optional flags that can modify the behavior of the command, and ‘directory_name(s)’ is the name(s) of the directory or directories you want to create.
Basic Usage and Examples
1. Creating a single directory
To create a new directory, simply type ‘mkdir’ followed by the name of the directory you want to create.
This will create a new directory named “new_directory” in the current working directory.
2. Creating multiple directories
You can create multiple directories simultaneously by specifying their names, separated by a space.
mkdir dir1 dir2 dir3
This command will create three new directories named “dir1,” “dir2,” and “dir3” in the current working directory.
3. Creating nested directories
The ‘-p’ (or ‘–parents’) option allows you to create nested directories in a single command. If any parent directories do not exist, they will be created as well.
mkdir -p dir1/dir2/dir3
This command creates a directory structure with “dir1” as the parent directory, “dir2” as a subdirectory of “dir1,” and “dir3” as a subdirectory of “dir2.”
4.Advanced Usage and Examples
a) Setting directory permissions
The ‘-m’ (or ‘–mode’) option allows you to set specific permissions for the newly created directory. Permissions can be set using octal notation, e.g., 755 for read, write, and execute permissions for the owner, and read and execute permissions for the group and others.
mkdir -m 755 secure_directory
This command creates a new directory named “secure_directory” with the specified permissions (rwxr-xr-x).
b) Verbose output
Using the ‘-v’ (or ‘–verbose’) option, you can display more information about the mkdir command’s actions.
mkdir -v verbose_example
This command will create a new directory named “verbose_example” and output a message like “mkdir: created directory ‘verbose_example'”.
3. Combining options
You can combine multiple options in a single command to perform complex tasks with the mkdir command. For example, you can create nested directories with specific permissions and display verbose output as follows:
mkdir -pv -m 750 nested_secure_directory/dirA/dirB
This command creates a nested directory structure, sets the specified permissions, and provides verbose output for each created directory.
The mkdir command is an essential tool for managing directories in Linux. By understanding its basic and advanced usage, you can efficiently create directories with specific permissions and structures. With the examples provided in this blog post, you can now confidently use the mkdir command to streamline your file management tasks in Linux.