mkdir Command in Linux with Examples

The mkdir command in Linux is used to create directories. It is a very useful command for organizing your files and folders.

Syntax:

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.

Options:

  • -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:

advertisement
advertisement

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.
Output:

advertisement
$ 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:

mkdir folder1/folder2/sample

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.

advertisement

Example:

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

Output:

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 --version

Output:

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:

mkdir day-*

This would create the following directories:

day-Monday
day-Tuesday
day-Wednesday
day-Thursday
day-Friday
day-Saturday
day-Sunday

More options

"-Z" : set the SELinux security context of each created directory to CTX

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.