Linux: How to Make a Directory Command

How do I make directory under Linux operating systems using the command prompt or bash shell?

You need to use the mkdir command to create new folders or directories under Linux operating systems. A directory (also known as folder in MS-Windows/macOS {OS X}) is nothing but a container for other directories and files. This page explains the basics of using the mkdir command on Linux.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements mkdir on Linux
Est. reading time 3 mintues

mkdir command Syntax

The mkdir command has the following syntax:
mkdir dirname
mkdir dirname1 dirname2
mkdir [option] dieNameHere
mkdir -p dir1/dir2/dir3


Let us see some commann useful examples.

How to create a new director

Open a terminal and then use the mkdir command to create empty directories. The following command would create a directory called foo:
$ mkdir foo
To list directories, enter:
$ ls
$ ls -l

The following command would create two directories within the current directory:
$ mkdir tom jerry
$ ls -l

How to create Directories in Linux

The -p option allows you to create parent directories as needed (if parent do not already exits). For example, you can create the following directory structure:
$ mkdir -p ~/public_html/images/trip
Verify it:
ls -l ~/public_html/
ls -l ~/public_html/images/
ls -R -l ~/public_html/

How to create directories in Linux with verbose option

Pass the -v as follows to display a message for each created directory:
mkdir -v dir1
ls -l

Setting up permissions when creating a directory

To set directory mode (permission) pass the -m option as follows:
mkdir -m {mode} dirName
The -m option is same as giving permissions using the chmod command. For examples:
mkdir data
chmod 0700 data

We can do the same with a single command and save typing time at the command-line:
mkdir -v -m 0700 data
ls -ld data

Setting up SELinux context with mkdir on RHEL or CentOS

The syntax is follows to set up system_u:object_r:httpd_sys_content_t:s0 as SELinux context for foo dir:

mkdir --context=system_u:object_r:httpd_sys_content_t:s0 foo
# verify it 
ls -ldZ foo

How to Create a Directory in Linux with mkdir Command with SELinux

Processes and files are labeled with an SELinux context that contains additional information, such as an SELinux user, role, type, and, optionally, a level. When running SELinux, all of this information is used to make access control decisions. In Red Hat Enterprise Linux, SELinux provides a combination of Role-Based Access Control (RBAC), Type Enforcement (TE), and, optionally, Multi-Level Security (MLS).

Sample mkdir demo command

Animated gif 01: mkdir in action under Linux / Unix like operating systems

Summing up

The mkdir command in Linux is used to make new directories as per your needs. We create a new directory in current directory or given path:
mkdir my-dir-name-here
ls -l

Also make directories recursively which is useful for creating nested dirs on Linux. For instance:
mkdir -p path/to/dir1/dir2

Getting help

Make sure you read the following man pages:
man mkdir
man ls
mkdir --help

Options summary
Option Description Example
-m (--mode=MODE) Set file mode (as in chmod command), not a=rwx – umask. mkdir -m 0644 sales
-p (--parents) No error if existing, make parent directories as needed. mkdir -p one/two/three
-v (--verbose) Print a message for each created directory. mkdir -v detla
-Z Set SELinux security context of each created directory to the default type. mkdir -Z dir1
--context[=CTX] Like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX. See above
--help Display this help and exit. mkdir --help
--version output version information and exit. mkdir --version

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • Abhishek Jan 10, 2014 @ 7:27

    I really love your concepts of .gif file, which first time anyone assumes, it is a video.

    Hats off to You :)

    And yes, thank you very much, Whenever I need to learn something regarding linux commands, I used to check from your website.

    Thank You Very Much For That

    • 🐧 Nix Craft Jan 10, 2014 @ 9:33

      You are welcome, and thanks for the kind comments!

  • Antony James Jan 20, 2014 @ 5:45

    Very Gooddd……….

    • osman Jun 2, 2016 @ 10:30

      Stop lying plss

  • varun vishal Jan 24, 2014 @ 8:46

    sahi hai boss

  • Ashay Feb 15, 2015 @ 5:03

    Thanks, I can now finally create a directory in my VPS.

  • Luis Aug 12, 2015 @ 16:34

    Thanks for share. this info was useful to me.

  • ravi kumar Jun 18, 2016 @ 2:01

    Can we find another way to create the nested directory in a single directory

  • courtney Nov 28, 2016 @ 14:28

    details is great really help me with my course work

  • राजेश चतुर्वेदी May 26, 2021 @ 23:13

    विवेक भाई,

    बहुत उपयोगी जानकारी। मैंने सीखा कि अब लिनक्स पर एक नई “directory” कैसे बनाई जाती है।

    आपका बहुत बहुत धन्यवाद।

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum