Creating Directories in Linux: A Beginner’s Guide to the mkdir Command

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

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

vijay@localhost ~]$ mkdir data
[vijay@localhost ~]$ mkdir /home/vijay/Documents/data
[vijay@localhost ~]$ ls 
data       Downloads  file.php   newfile1  newfile4  Templates  Videos
Desktop    file1      Music      newfile2  Pictures  tfile
Documents  file2      nano.save  newfile3  Public    toucfile
[vijay@localhost ~]$ 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.

[vijay@localhost ~]$ mkdir /root/data
mkdir: cannot create directory ‘/root/data’: Permission denied
[vijay@localhost ~]$

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:

[vijay@localhost ~]$ mkdir -v data1
mkdir: created directory 'data1'
[vijay@localhost ~]$

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

[vijay@localhost ~]$ 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
[vijay@localhost ~]$ 
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

[vijay@localhost ~]$ mkdir -m777 data2
[vijay@localhost ~]$ 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
[vijay@localhost ~]$

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.

[vijay@localhost ~]$ cd /home/vijay/Documents/
[vijay@localhost Documents]$ ls
data  file1  file2  file.php
[vijay@localhost Documents]$ cd data/
[vijay@localhost data]$ ls
[vijay@localhost data]$ mkdir dir1 dir2 dir3
[vijay@localhost data]$ ls
dir1  dir2  dir3
[vijay@localhost 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

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

6 directories, 0 files
[vijay@localhost Documents]$

Now I am doing same job by using -p option

[vijay@localhost Documents]$ tree data
data
├── dir1
│   └── dirp1
│       └── dirp2
│           └── dirp3
├── dir2
│   └── dirp1
│       └── dirp2
│           └── dirp3
│               └── dirp4
└── dir3

10 directories, 0 files
[vijay@localhost 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 $@
cd $@directoryname
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.
  • $@ (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

[vijay@localhost Documents]$ nano mkdir.sh 
[vijay@localhost Documents]$ ./mkdir.sh 
[vijay@localhost Documents]$ ls
data  file1  file2  file.php  mkdir.sh  vijay
[vijay@localhost Documents]$ ls vijay/
script
[vijay@localhost 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.

If you like our content, please consider buying us a coffee.
Thank you for your support!