Skip to main content

Commands

Make Directories with mkdir Command

The mkdir command is used for making directories (folders) in the Linux command line. Learn to use mkdir command in Ubuntu in this tutorial.

Creating directories is one of the easiest yet important tasks.

In Linux, you use the mkdir command to create new directories and in this tutorial, I will walk you through multiple examples of creating a directory using the mkdir command.

So let's start with the syntax.

How to use the mkdir command in Ubuntu

If you want to learn to use the mkdir command (or any other command), you should start by learning its syntax:

mkdir [options] directory_name

Here,

  • [options]: is used to tweak the default behavior of the mkdir command.
  • directory_name: is where you provide the path where you want to create a directory with a directory name.

When you use the mkdir command without any options or giving a path, it will create a new directory inside of the current working directory:

mkdir Dir_name
Create new directory using the mkdir command

But it can do a lot more than just create a directory inside of a current working directory.

Let me show you how.

1. Create a directory at a different location

To create a directory in a different location than the current directory, you have to provide an absolute or relative path with the name of the directory:

mkdir /path/to/directory_name

For example, here, I created a new directory named Hello inside the test directory:

Create directory at specific path

2. Create multiple directories

To create multiple directories using the mkdir command, all you have to do is separate the directory names by space:

mkdir dir-1 dir-2 dir-3

Also, if you want to create multiple directories at different locations, you have to add a path along with the name of the directory:

mkdir /path/to/dir-1 /path/to/dir-2 /path/to/dir-3

For example, here I created one directory named Test inside the current directory and the other one named MyDir inside the LU directory:

mkdir Test ~/LU/MyDir
Create multiple directories at different locations using the mkdir command

3. Create parent directories

Let's suppose you want to create a directory ~/Downloads/Matrix/567.

Now, if any of the shown directories like Matrix or Downloads does not exist in the given path, it will throw an error saying the directory does not exist:

Can not create directory no such directory exist

In that case, you have to use the -p flag and it will create the parent directories as well as the specified directory:

mkdir -p /path/to/directory

For example, I want to create a directory 567 at /Downloads/Matrix. But the thing is Matrix directory does not exist!

So when I will use the -p flag, it will create the Matrix directory and then will create 567:

mkdir -p ~/Downloads/Matrix/567
Create parent directory using the mkdir command

4. Set permissions while creating a directory

When a normal user creates a directory, it typically has the following permissions:

Check directory permissions in Ubuntu Linux

For some users, these default permissions won't get the job done so what they do is first create a directory and then change the permissions.

But not anymore!

The mkdir command allows you to define directory permissions while creating the directory itself.

For that, you need to use the -m (mode) flag and then specify the permissions in numerical form as shown:

mkdir -m 700 New_Directory

For example, here, I created a directory named LU with permissions set to 700 (only the owner has the right to read, write, and execute):

mkdir -m 700 LU
Set permissions while creating directory using the mkdir command

Check the directory size in Ubuntu

Once you are done creating the directory, you will store multiple files inside but what if you want to check the size of that directory?

Well, here's a detailed guide on how you check the size of a directory:

How to Check Directory Size in Ubuntu Command Line
Not getting the directory size with the ls command? Here’s how to check the folder size in the Ubuntu command line.

I hope you will find this helpful.