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
[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:
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:
For example, here, I created a new directory named
Hello inside the
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
mkdir Test ~/LU/MyDir
3. Create parent directories
Let's suppose you want to create a directory
Now, if any of the shown directories like
Downloads does not exist in the given path, it will throw an error saying the directory does not 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
/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
mkdir -p ~/Downloads/Matrix/567
4. Set permissions while creating a directory
When a normal user creates a directory, it typically has the following permissions:
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
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:
I hope you will find this helpful.