The mkdir command in Linux is used to create directories. It is a very useful command for organizing your files and folders.
The syntax for the mkdir command is as follows:
mkdir [options] [directory name]
- options: Optional arguments that can be used to change the behavior of the mkdir command.
- directory name: The name of the directory to be created.
- -p: Create parent directories if they don’t exist.
- -m: Set permissions for the new directory.
- -v: Show verbose output, including the names of the created directories.
mkdir Command Examples:
Here is a list of examples demonstrating the usage of the “mkdir” command:
Example 1: Creating a Single Directory
$ mkdir sample
This command creates a single directory named “sample” in the current directory.
Example 2: Create Multiple Directories
$ mkdir folder1 folder2 folder3
This command creates three directories named “folder1”, “folder2”, and “folder3” in the current directory.
Example 3: Control Directory Permissions
$ mkdir -m 777 sample
Creates a directory named “sample” with read, write, and execute permissions for the owner, group, and other users. The ls -l command displays permission information.
$ ls -l total 4 drwxrwxrwx 2 himanshu himanshu 4096 Jul 7 19:31 sample
Example 4: Create Intermediate Directories with mkdir -p
Use mkdir -p to create a new directory and all of its intermediate directories if they do not already exist. For example, to create folder1/folder2/sample, run:
If the directory folder1 does not exist, it will be created. If the directory folder2 does not exist, it will be created within the directory folder1. And finally, the directory sample will be created within the directory folder2.
sanfoundry-> mkdir folder1/folder2/sample mkdir: cannot create directory `folder1/folder2/sample': No such file or directory sanfoundry-> mkdir -p folder1/folder2/sample sanfoundry-> cd folder1/folder2/ sanfoundry-> ls sample
The mkdir -p command is a very useful for creating directories, especially when you are not sure if the intermediate directories already exist.
Example 5: Display Verbose Message
To display a verbose message for each directory created, use the mkdir -v option. For example, to create the directory “folder1/folder2/sample” and display a verbose message for each directory created, you would use the following command:
mkdir -pv folder1/folder2/sample
mkdir: created directory `folder1' mkdir: created directory `folder1/folder2' mkdir: created directory `folder1/folder2/sample'
Example 6: Check Version
To get version information for the mkdir command, use the mkdir –version option.
mkdir (GNU coreutils) 8.13 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
This command shows the version information for the mkdir command, indicating the version number and copyright details.
Using mkdir with Wildcards
You can also use wildcards with the mkdir command. For example, to create a directory for each day of the week, you would use the following command:
This would create the following directories:
day-Monday day-Tuesday day-Wednesday day-Thursday day-Friday day-Saturday day-Sunday
"-Z" : set the SELinux security context of each created directory to CTX
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.