How to Use 'mkdir' to Create Linux Directories

Make folders from a terminal with this simple command

What to Know

  • To create directories in Linux, you can open Terminal and use the command line with the mkdir command.
  • Once you create a new directory in Linux, then you can change permissions and create folders within the directory.
  • You can also create a directory and set permissions at the same time.

You can create new directories in Linux using the command line. The command to create directories is mkdir. Here is how to create directories with mkdir.

Animated illustration of how to create a new directory in Linux.
Lifewire / Nusha Ashjaee

This article also looks at the switches used with mkdir and the proper syntax required to make the command work correctly.

How to Create a New Directory

The simplest way to create a new directory is to enter the following command:

mkdir <foldername>

For example, to create a directory called test, open a terminal window, navigate to the folder where you want the new folder, then enter:

mkdir test
mkdir command in Ubuntu

Change the Permissions of the New Directory

After creating a new folder, set the permissions so that only a certain user can access it or so that some people can edit files in the folder, but others have read-only permissions.

To continue with the example above, run the ls command to see the permissions for the folder called test:

ls -lt
ls -lt command in Ubuntu

Run the ls command in the folder where the test folder is located. If it's your home directory (for example, you didn't use the cd command), then you don't have to change the directory here.

You should see something like this (but probably with several other entries, considering that there are other folders there):

drwxr-xr-x 2 owner group 4096 Jan 22 10:43 test

The permissions are drwxrwxr-x, 2, owner, and group.

    • The d indicates 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 read.
    • w is for write.
    • x is for execute (which means you can access the folder).
  • The next three characters are the group permissions for the file specified by the group name. The options are r, w, and x. The hyphen means that a permission is missing. In the example above, anybody belonging to the group can access the folder and see the files, but can't write to the folder.
  • The final characters are the permissions that all users have, and these are the same as the group permissions.

To change the permissions for a file or folder, use the chmod command. The chmod command lets you specify three numbers which set the permissions:

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

Add the numbers together for a mixture of permissions. For example, to attribute read and execute permissions, the number is 5 (4+1), or for read and write permissions, the number is 6 (4+2).

You must specify three numbers as part of the chmod command. The first number is for the owner permissions, the second is for the group permissions, and the last is for everyone else.

For example, for the owner to have full permissions, the group to have read and execute permissions, and anyone else to have no permissions, enter the following:

chmod 750 test

Use the chgrp command to change the group name that owns a folder. For example, to create a directory that the accountants in a company can access, first make the group accounts by typing the following:

groupadd accounts

If you don't have the correct permission to create a group, use sudo to gain extra privileges or use the su command to switch to an account with valid permissions.

Next, change the group for a folder by typing the following:

chgrp accounts <foldername>

For example:

chgrp accounts test

To give the owner and everybody else in the accounts group read, write, and execute, but read-only access to others, use the following command:

chmod 770 test

How to Create a Directory and Set Permissions at the Same Time

You can create a directory and set the permissions for that directory at the same time using the following command:

mkdir -m777 <foldername>

This command creates a folder that everybody can access. It's rare to create folders with this kind of permission.

Create a Folder and Any Parents That Are Required

You can create a directory structure without creating each individual folder, for example, to create folders for music as follows:

  • /home/music/rock/alicecooper
  • /home/music/rock/queen
  • /home/music/rap/drdre
  • /home/music/jazz/louisjordan

It takes time to create the rock folder for alice cooper and queen, followed by the rap and jazz folders for the others.

By specifying -p, you can create all the parent folders on the fly if these don't exist:

mkdir -p <foldername>

For example, this mkdir command makes one of the folders listed above:

mkdir -p ~/music/rock/alicecooper

Get Confirmation That a Directory Was Created

By default, the mkdir command doesn't tell you if the directory was created successfully. Usually, if no errors are shown, 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>

The output will be similar to this: 

mkdir: created directory /path

Use mkdir in a Shell Script

You can also use the mkdir command as part of a shell script, for example, a script that accepts a path. When the script is executed, it creates the folder and adds a single text file called hello.

#!/bin/bash
mkdir $@
cd $@
touch hello
  • The first line should be included in every script that you write. It's used to show that this is a BASH script.
  • mkdir creates the folder.
  • $@ (also known as input parameters) put at the end of the second and third lines is replaced with the value you specify when running the script.
  • cd changes to the directory you specify.
  • touch creates an empty file called hello.