Basic mkdir command examples in Linux

mkdir command examples

In this article, we’ll cover the mkdir command, which stands for make directories, and review basic mkdir command examples.

Table of Contents

Introduction

David Mackenzie authored the mkdir command found in the GNU coreutils package, as documented in the Linux Manual pages. This command, originating in the 1970s, has become ubiquitous across various operating systems, including Linux, DOS, Microsoft Windows, IBM, and OS/2. Its enduring presence underscores its versatility and widespread adoption beyond just Linux environments.

Some basic mkdir examples

It is not uncommon to use the mkdir command when performing Linux-related procedures. For example, a recent SSM article we posted some days ago (as of the date of this publication), required that we create a directory in order to mount a logical volume.

The article about Installing and Using Git in Linux also requires the use of the mkdir command at some point in the procedure.

Let’s go over a few basic mkdir command examples.

mkdir -p [name_of_directory]

This command will create a parent directory if it doesn’t exist. If the directory exists it will not throw an error. For example, a command like (below) will generate an error because the testparent and test1 directories don’t exist:

				
					$ mkdir testparent/test1/test2
mkdir: cannot create directory ‘testparent/test1/test2’: No such file or directory
				
			

However, when we execute the same command with the -p option,  both testparent and test1 directories are created as well along with test2 (as shown in the image below).

mkdir command example #1

Photo by admingeek from Infotechys

mkdir -v [name_of_directory]

To -v option, which stands for verbose, outputs a message to your terminal screen about the directory created using the mkdir command.
				
					$ mkdir -v testparent/test1/test2/test3
mkdir: created directory ‘testparent/test1/test2/test3’
				
			

In the above example, we created another sub-directory under testparent/test1/test2 called test3.

mkdir version

To check the version of mkdir installed on our machine, we run the mkdir command with the –version option.

				
					$ mkdir --version
mkdir (GNU coreutils) 8.22
Copyright (C) 2013 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.

Written by David MacKenzie.
				
			

mkdir and file modes

The -m option, which sets the file modes, is used with the mkdir command, when we want to specify permissions or user access to a certain directory or directories.

In the screenshot below, we created yet another sub-directory under testparent/test1/test2/test3 and passed to it the -m option along with the a=rwx. This grants read, write, and execute permissions to the test4 sub-directory to all users.

mkdir command and file modes

Photo by admingeek from Infotechys

Create multiple directories using mkdir

To create multiples directories in given location of the directory tree, we execute the mkdir command using the curly brackets.

The following command while create subtest1, subtest2, and subtest3 sub-directories under the testparent/test1 parent directory.

				
					$ mkdir testparent/test1/{subtest1,subtest2,subtest3}
[admin@vm4-dev-infotechys ~]$ ls -l testparent/test1
total 0
drwxrwxr-x. 2 admin admin 6 Feb 5 03:14 subtest1
drwxrwxr-x. 2 admin admin 6 Feb 5 03:14 subtest2
drwxrwxr-x. 2 admin admin 6 Feb 5 03:14 subtest3
drwxrwxr-x. 3 admin admin 19 Feb 5 02:12 test2
				
			

NOTICE: The sub-directories are separated by commas and no spaces.

Conclusion

With its straightforward syntax and indispensable functionality, mkdir exemplifies the elegance and efficiency of Unix-like commands. Whether you’re a seasoned sysadmin or a curious enthusiast, mastering the art of mkdir opens doors to effective file management and organization across a myriad of computing platforms.

Was this article informative or helpful to you? If so, leave us a comment below and share!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *