How to Use 'mkdir' to Create Linux Directories

Make folders from a terminal with this simple command

You can create new folders/directories in Linux using the command line. The command that you use for creating directories is mkdir.

Below is a full look at how to create directories with mkdir. We'll also look at the switches you can use with mkdir and the proper syntax you need to use to make the command work correctly.

How to Create a New Directory

An illustration of how to create a new directory in Linux.
Nusha Ashjaee ©Lifewire 

The simplest way to create a new directory is as follows:

mkdir <foldername> 

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

mkdir test 
mkdir command in Ubuntu

The cd ~ command is used to change the working directory so that when you enter the mkdir command, the folder is created exactly where you want it to be.

Changing the Permissions of the New Directory

After creating a new folder you might wish to 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, we'll run the ls command to see the permissions for the folder called test:

ls -lt 
ls -lt command in Ubuntu

You must run the ls command in whatever folder the test folder is located in. If it's your home directory (i.e., you didn't use the cd command above), then you don't have to worry about changing the directory here.

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

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

The bits we are interested in are drwxrwxr-x 2 owner and group.

  • 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 read
    • w is for write
    • x is for execute (which in this case means you can access the folder)
  • The next three characters are the group permissions for the file specified by the group name. Again the options are r, w, and x. The hyphen means that there is a permission 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 as you can see in the example above, these are the same as the group permissions.

To change the permissions for a file or folder you can 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 you need is 5 (4+1), or for read and write permissions, you'd use the number is 6 (4+2).

Remember, you need to 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 instance, for the owner to have full permissions, the group read and execute, and no permissions for anyone else, you'd type the following:

chmod 750 test 

Use the chgrp command to change the group name that owns a folder. For example, imagine you want to create a directory that all the accountants in your company can access.

You'd first make the group accounts by typing the following:

groupadd accounts 

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

Now, you can 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 only read-only access to others, you'd 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> 

The above command will create a folder which everybody has access to. It's very rare that you would want to create anything with this kind of permissions.

Create a Folder and Any Parents That Are Required

Imagine you want to create a directory structure but you don't want to create each individual folder along with the way.

For example, you might be creating folders for your music as follows:

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

It would be annoying to have 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 they don't already exist:

mkdir -p <foldername> 

For example, this mkdir command is used to make one of the folders listed above:

mkdir -p ~/music/rock/alicecooper 

Getting Confirmation That a Directory Was Created

By default, the mkdir command doesn't tell you if the directory you're making was successfully created. 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> 

The output will be along the lines of 

mkdir: created directory /path/to/directoryname

Using mkdir in a Shell Script

You can also use the mkdir command as part of a shell script.

For our example, we'll look at a script that accepts a path. When the script is executed it will create the folder and add 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 indeed a BASH script.

  • mkdir is, of course, used to create the folder.
  • $@ (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 hello.

Follow these steps to try the script out for yourself: