mkdir-command-to-create-a-directory-in-linux-terminal

How to use mkdir command to create a directory in Linux beginner’s guide

If you are looking at how to make a directory in Linux then you are at the right place, use mkdir command to create a directory in Linux by using the terminal.

You are using a graphical interface to create a folder in windows, but you must use the command to create a folder in Linux. I will teach you to create a new directory, multiple directories, a directory with full permission, etc.

Article Contents:

Introduction of mkdir command in Linux

mkdir is a single command to create a directory in Linux/Unix based all distributions. It may be debian, Ubuntu, RHEL, CentOS, Fedora, Arch Linux and more.

If you are using any Linux operating system, Keep in mind you must use mkdir command for creating folders in the current directory as well as the desired location.

mkdir is short form of make directory, and used to create directory through terminal.

You can use man command to refer manual to know more information.

$man mkdir

The basic syntax for the mkdir command is as follows:

mkdir [OPTION] [DIRECTORY]

The command takes one or more directory names as its arguments.

Create a new directory in Linux

Use mkdir command to create a directory in Linux followed by the directory. If you want to create a new directory with name newdir you would run the following command:

$mkdir newdir

Type “mkdir [newdir]” at the terminal to make the directory. give the name of your new directory in place of the [newdir] command line operator.

For example, to create a directory called “data,” type “mkdir data.”

Be aware that this will create the directory within the current working directory, you can use pwd command to check the current working directory.

After creating directory use ls command to check the content of the directory.

If you are looking to create directory in another location then use mkdir command followed by location path and new directory.

For example, I am willing to create a new directory named “data” inside /home/vijay/Documents/ then the command will be as follows.

$mkdir /home/vijay/Documents/data

[email protected] ~]$ mkdir data
[[email protected] ~]$ mkdir /home/vijay/Documents/data
[[email protected] ~]$ ls 
data       Downloads  file.php   newfile1  newfile4  Templates  Videos
Desktop    file1      Music      newfile2  Pictures  tfile
Documents  file2      nano.save  newfile3  Public    toucfile
[[email protected] ~]$ ls /home/vijay/Documents/
data  file1  file2  file.php

You are not allowed to create a new directory inside directory, where you don’t have permission.

For Example, You are a normal user then you can not make folder inside /root directory.

You will get the following error.

[[email protected] ~]$ mkdir /root/data
mkdir: cannot create directory ‘/root/data’: Permission denied
[[email protected] ~]$

Get confirmation to make a directory in Linux

By default, You will not get any confirmation of compeleting mkdir command or making directory successfuly, Usually, if no errors are shown then you can assume it worked.

However, if you want more verbose output so that you know what’s been created, use the -v switch:

$mkdir -v Foldername
or
$mkdir -v /path/to/location/foldername

See the example with output as follows:

[[email protected] ~]$ mkdir -v data1
mkdir: created directory 'data1'
[[email protected] ~]$

Create a directory with permissions in Linux

I have created a new folder named data in the previous example, some permission has been allotted to this folder.

You can use ls command followed by -lt option to check the certain permission of newly created folder data. The command will be as follows:

$ls -lt

You you can see outpust something like follows. you will see several other entries too, because of other folders there as well:

Result here

[[email protected] ~]$ ls -lt
total 28
drwxrwxr-x. 2 vijay vijay   6 Apr  4 19:00 data1
drwxr-xr-x. 3 vijay vijay  60 Apr  4 18:22 Documents
drwxrwxr-x. 2 vijay vijay   6 Apr  4 18:21 data
-rw-------. 1 vijay vijay   1 Apr  3 16:58 nano.save
-rw-rw-r--. 1 vijay vijay   1 Apr  2 15:10 file.php
-rw-rw-r--. 1 vijay vijay 238 Mar 31 17:50 newfile3
-rw-rw-r--. 1 vijay vijay   0 Mar 31 17:50 newfile4
-rw-rw-r--. 1 vijay vijay 127 Mar 31 17:46 newfile1
-rw-rw-r--. 1 vijay vijay 127 Mar 31 17:46 file1
-rw-rw-r--. 1 vijay vijay   0 Mar 31 17:38 toucfile
drwxr-xr-x. 2 vijay vijay  25 Mar 29 20:48 Desktop
-rw-rw-r--. 1 vijay vijay 238 Mar 29 14:27 newfile2
-rw-rw-r--. 1 vijay vijay 111 Mar 29 13:08 file2
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Music
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Pictures
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Videos
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Downloads
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Public
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Templates
-rw-rw-r--. 1 vijay vijay   0 May 10  1984 tfile
[[email protected] ~]$ 
drwxrwxr-x. 2 vijay vijay   6 Apr  4 19:00 data1

I am going describe a short descripption on permissions. it is as follows:

  • The d tells us that test is a directory.
  • The first three letters following the d are the owner permissions for the directory specified by the owner’s name:
  • r is for reading
  • w is for write
  • x is for executing (which in this case means you can access the folder)
  • (-) the hyphen means this permission is missing. No permission.

Three permissions:

  • First permission for owner
  • Second, for Group, anybody belonging to the group has the same permissions.
  • Third for other users

I am going to create a directory with full permission then use -m option followed by permission set.

Three numbers which set the permissions:

  • Read = 4
  • Write = 2
  • Execute = 1

Full permission means user can read, write and execute, So it would be in numbers

Read+Write+Execute = 4+2+1 = 7, So use 777 for full permission.

$mkdir -m777 folder_name
or
$mkdir -m777 /path/to/location/folder_name

[[email protected] ~]$ mkdir -m777 data2
[[email protected] ~]$ ls -lt
total 28
drwxrwxrwx. 2 vijay vijay   6 Apr  4 19:51 data2
drwxrwxr-x. 2 vijay vijay   6 Apr  4 19:00 data1
drwxr-xr-x. 3 vijay vijay  60 Apr  4 18:22 Documents
drwxrwxr-x. 2 vijay vijay   6 Apr  4 18:21 data
-rw-------. 1 vijay vijay   1 Apr  3 16:58 nano.save
-rw-rw-r--. 1 vijay vijay   1 Apr  2 15:10 file.php
-rw-rw-r--. 1 vijay vijay 238 Mar 31 17:50 newfile3
-rw-rw-r--. 1 vijay vijay   0 Mar 31 17:50 newfile4
-rw-rw-r--. 1 vijay vijay 127 Mar 31 17:46 newfile1
-rw-rw-r--. 1 vijay vijay 127 Mar 31 17:46 file1
-rw-rw-r--. 1 vijay vijay   0 Mar 31 17:38 toucfile
drwxr-xr-x. 2 vijay vijay  25 Mar 29 20:48 Desktop
-rw-rw-r--. 1 vijay vijay 238 Mar 29 14:27 newfile2
-rw-rw-r--. 1 vijay vijay 111 Mar 29 13:08 file2
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Music
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Pictures
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Videos
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Downloads
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Public
drwxr-xr-x. 2 vijay vijay   6 Mar 21 11:17 Templates
-rw-rw-r--. 1 vijay vijay   0 May 10  1984 tfile
[[email protected] ~]$

Create multiple directories in Linux

My boss told me create five directory inside data directory, data directory location is /home/vijay/Documents.

First I will use cd command to switch the given location.

$cd /home/vijay/Documents

Let,s see data directory is exist here or not, so use ls command. If yes then go inside it by using cd command again. This time just use cd data command

Now I create multiple directories on the current working directory, use mkdir command followed by directories names. You must have a single space between the folder’s name.

[[email protected] ~]$ cd /home/vijay/Documents/
[[email protected] Documents]$ ls
data  file1  file2  file.php
[[email protected] Documents]$ cd data/
[[email protected] data]$ ls
[[email protected] data]$ mkdir dir1 dir2 dir3
[[email protected] data]$ ls
dir1  dir2  dir3
[[email protected] data]$

Create a Folder and Any Parents That Are Required

If you want to to create parent directories using the mkdir command pass the -p option.

Suppose that I want to create subdirectories inside a parent directory dir1 and path would be dir1/dirp1/dirp2/dirp3 is to be created. This can be created with mkdir as follows in normal way:

cd dir1
mkdir dirp1
cd dirp1
mkdir dirp2
cd dirp2
mkdir dirp3

Now see the directory tree by using tree command

[[email protected] data]$ cd dir1
[[email protected] dir1]$ mkdir dirp1
[[email protected] dir1]$ cd dirp1/
[[email protected] dirp1]$ mkdir dirp2
[[email protected] dirp1]$ cd dirp2/
[[email protected] dirp2]$ mkdir dirp3
[[email protected] dirp2]$ cd /home/vijay/Documents/
[[email protected] Documents]$ tree data
data
├── dir1
│   └── dirp1
│       └── dirp2
│           └── dirp3
├── dir2
└── dir3

6 directories, 0 files
[[email protected] Documents]$

Now I am doing same job by using -p option

[[email protected] Documents]$ tree data
data
├── dir1
│   └── dirp1
│       └── dirp2
│           └── dirp3
├── dir2
│   └── dirp1
│       └── dirp2
│           └── dirp3
│               └── dirp4
└── dir3

10 directories, 0 files
[[email protected] Documents]$

Use a shell script to create a folder in Linux

As you know shell script is used to create executable file, it has bunch of commands. You can also use the mkdir command as part of a shell script.

For example, I am going to create a simple script which accepts a path. When the script is executed it will create the folder and add a single text file called “script” with the help of touch command.

#!/bin/bash
mkdir directoryname [email protected]
cd [email protected]
touch script

The first line (#!/bin/bash) should be included in every script that you write. This line difine a file as bash script.

  • mkdir is used to create the folder, replace directoryname with your disired directory name.
  • [email protected] (also known as input parameters) put at the end of the second and third line is replaced with the value you specify when running the script.
  • cd changes into the directory you specify.
  • touch creates an empty file called script.

Now its your turn to create a script and run it.

I am going to create a directory name vijay, and create a blank file named script inside newly created directory vijay

So My content will be as follows:

#!/bin/bash
mkdir vijay
cd vijay
touch script

[[email protected] Documents]$ nano mkdir.sh 
[[email protected] Documents]$ ./mkdir.sh 
[[email protected] Documents]$ ls
data  file1  file2  file.php  mkdir.sh  vijay
[[email protected] Documents]$ ls vijay/
script
[[email protected] Documents]$ 

Use a File Manager to create a directory in Linux

Most of Linux distribution has graphical interface inbuilt now a days, So you can use it similar as windows, and create a folder in linux easily.

So my dear friend, use graphical interface and enjoy. Frankly I told you again command line interface 1000 times better then graphical interface.

You may understand later, when you become a pro Linux user. By the way leave this topic. If you want to use graphical interface follow the given steps:

Step 1: Open file manager, If you are using cent-OS then click on Activities and click on files.

6-open-file-manager-in-centos

Step 2: go to to the location where you want to create folder. In Example, I will create new folder inside “data” directory.

Use double click on desired directory to open it.

7-open-desired-location

Step 3: Write click on mouse, anywhere inside the file manager screen. And click on “New Folder” option.

8-create-new-folder

Step 4: Now new window will be displayed on the screen, give the directory name and click on create.

9-give-folder-name

New windows will be created, You can see in below image.

9-folder-has-been-created

Conclusion

The mkdir command in Linux is used to create new directories.

For more information about mkdir, visit the mkdir man page.

If you have questions, feel free to leave a comment below.

Special Offer on Ethical hacking Course in Hindi

Ethical-hacking-course-in-hindi-50-off

If Appreciate My Work, You should consider: