How to use mkdir command on Ubuntu Linux with Examples

The mkdir command on Ubuntu allow user to create new directories if they do not already exist on the file systems.

This post shows new users and students what the mkdir command is used for and how to use it. For students and new users learning how to use Linux systems, the easiest place to start might be on Ubuntu Linux OS.

Ubuntu is an open-source Linux operating systems that runs on desktops, laptops, server and other devices.

When you’re ready to learn how to use the mkdir commands, follow the guide below:

About mkdir command:

The mkdir command on Ubuntu allow user to create new directories if they do not already exist on the file systems. Like using your mouse and keyboard to create new folders. the mkdir is the way to do it on the command line.

Syntax:

The syntax is the rule and format of how the mkdir command can be used. the systax can options can be reordered. but straight format must be followed.,.

Below is an example syntax of how to use the mkdir comamnd.

mkdir [OPTION]. DIRECTORY.

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the command. they are separated by spaces and followed after the commands options.

Below are some options of the mkdir command:

DIRECTORY. Replace DIRECTORY .. with the name of the directory you want to create. If the specified directory does not already exist, mkdir creates it..
-m=MODE,
–mode=MODE
Use the -m or –mode option set file mode (as in chmod), not a=rwx – umask. The syntax of MODE is the same as with the chmod command.
-p,
–parents
Use the -p or –parents option to make parent directories as needed
-v,
–verbose
Verbose output. Print a message for each created directory.
 -Z Use the -Z option to set SELinux security context of each created directory
to the default type
–help Use the –help option with the command to display this help and exit
–version  Use the –version option to output version information and exit

Examples:

Below are some examples of how to run and use the mkdir on Ubuntu Linux.

Run the command below create a new directory called Confidential without any mkdir command options. This will just createa a simple directory or folder called Confidential..

mkdir Confidential

You can use the mkdir command with the -m or –mode option to create a directory and specify the permissions as well. For example, to createa a directory or folder called Confidential and set a permission, run the commands below.

mkdir -m a=rwx Confidential

The above command will create a Confidential directory and give all users = read , write, and execute permissions. (a = read, write, execute)

When you run mkdir with the –help option, you’ll see the help text below:

Usage: mkdir [OPTION]. DIRECTORY.
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z                   set SELinux security context of each created directory
                         to the default type
      --context[=CTX]  like -Z, or if CTX is specified then set the SELinux
                         or SMACK security context to CTX
      --help     display this help and exit
      --version  output version information and exit

That’s it!

Hope you like it and please come back soon for more Ubuntu Linux command!