Ubuntu mkdir – Linux: Create directory or folder using mkdir command

Linux: create directory or folder using mkdir command

ubuntu mkdir: In this article we will discuss how to create directory or folder using mkdir command. In Linux system, we can create new directories either from the command line or with the help of our desktop’s file manager.

mkdir :

  • It allows users to create or make new directories.
  • mkdir stands for make directory.

mkdir options :

  • -p, -parents : Add directory including its sub directory.
  • -v, -verbose : Print a message for each created directory.
  • -m, -mode : Set access privilege.

mkdir command syntax in linux :

The basic command for creating directories in Linux consist of the mkdir command and the name of the directory.

Syntax:
mkdir<dirname>

Note: if you will not provide a path then by default your file will be create in your current directory only. And if you want to create your directory somewhere else then you have to provide the path.

 Create a single directory in Linux with mkdir command :

To create a single directory in Linux, just provide folder name along with mkdir command.

Example:

ubuntu@Linux-VM:~/study$ mkdir project

It will create directory name ‘student’

Create multiple directories in Linux with mkdir command :

 If you want to create multiple directories in one command then provide all the names of the directories to mkdir command separated by space.

ubuntu@Linux-VM:~/study/student$ mkdir name rollno class section mobile_no

It created 5 directories i.e ‘name’, ‘rollno’, ‘class’, ‘section’, ‘mobile_no

We can also use verbose option(-v) which will help to create multiple directories. It will give a message after creating each directory.

Example:

ubuntu@Linux-VM:~/study$ mkdir -v class1 class2 class3
mkdir: created dictionary 'class1'
mkdir: created dictionary 'class2'
mkdir: created dictionary 'class3'
ubuntu@Linux-VM:~/study$

If we are trying to create a directory which is already exist then by default mkdir gives error. Let’s see with an example:

ubuntu@Linux-VM:~/study/student$ ls
name rollno class section mobile_no
ubuntu@Linux-VM:~/study/student$ mkdir section
mkdir: cannot create directory 'section': File exists

If we want a behavior which will not throws error if the directory is already exist and creates new directory if not exist then there is an option i.e -p option.

Example:

ubuntu@Linux-VM:~/study/student$ ls
name rollno class section mobile_no
ubuntu@Linux-VM:~/study/student$ mkdir section
ubuntu@Linux-VM:~/study/student$

Linux: create directory and subdirectories in one command – recursively : 

If we want to create a folder and subfolder in a single command then we need to provide -p option.

-p stands for parents, it means if a complete folder path is given and and any folder in the path is missing then it it create it. Let’s understand with an example suppose we want to create a folder ‘book’ and that the folder ‘book’ create folder ‘chapter’ and inside folder ‘chapter’ create folder ‘lines’. Nested folder structure should be book/chapter/lines. But if we try to create this nested folder with mkdir command then it will throw an error.

ubuntu@Linux-VM:~/study$ mkdir book/chapter/lines
mkdir: cannot create directory 'book/chapter/lines': No such file or directory
ubuntu@Linux-VM:~/study$

From the above example we understand that mkdir command cannot create missing parent folders by default. To automatically create missing parent folders we have use -p option with mkdir option.

ubuntu@Linux-VM:~/study$ mkdir -p book/chapter/lines

Linux: create directory with space in name :

 Example:

ubuntu@Linux-VM:~/study$ mkdir class test
ubuntu@Linux-VM:~/study$ ls
class test
ubuntu@Linux-VM:~/study$

From the above example we want to make a directory ‘class test’ but just giving the space it creates two directories i.e ‘class’ and ‘test’ which we don’t want it. So here it tricky to make a directory with space in name. we can’t directly create directory using mkdir. So we have to escape the spaces in the name while passing it to the mkdir command.

ubuntu@Linux-VM:~/study$ mkdir class\ test
ubuntu@Linux-VM:~/study$ mkdir 'course work'
ubuntu@Linux-VM:~/study$ ls
'class test' 'course work'
ubuntu@Linux-VM:~/study$

Create a dictionary with today’s date :

ubuntu@Linux-VM:~/study$ mkdir $(date +%Y-%m-%d)
ubuntu@Linux-VM:~/study$ ls
2021-04-30

Create a dictionary with today’s date and time :

 ubuntu@Linux-VM:~/study$ mkdir $(date +%Y-%m-%d--%H-%M-%S)
ubuntu@Linux-VM:~/study$ ls
2021-04-30—16-38-45