How to Use the mkdir Command in Linux

29/12/2020
Chưa phân loại

The mkdir command is used to make new directories in Linux.  In this article, I am going to show you how to use the mkdir command to create directories from the command line in Linux. I am going to use Ubuntu 18.04 LTS for the demonstration, but any Linux distribution should be fine to try out the examples given here. So, let’s get started.

Absolute Path and Relative Path:

There are 2 types of path in Linux. Absolute path and relative path. Having clear concepts of these terms are essential to working with the mkdir command.

Absolute path: It is the full path to your desired directory or file. An absolute path contains the / (root) directory first and then moves downward the directories hierarchy (tree) until your desired directory or file is reached.

For example, /db/mysql/data is an absolute directory path. /etc/fstab is an absolute file path.

Relative path: Relative path is calculated from the current working directory. It may start with or without ./ but it can’t start with /

You can print the current working directory path with the pwd command as follows:

Now, if the directory path is ./db/mysql/data or simply db/mysql/data then, it’s actually inside the parent directory /home/shovon. Notice how we don’t have to type in the full or absolute path /home/shovon/db/mysql/data.

Absolute paths make working with mkdir command a lot easier.

Creating a Directory with mkdir:

This is the simplest and the most common usage of mkdir. You can create a new directory with mkdir very easily.

To create a new directory files/ (let’s say) in your current working directory, run the following command:

$ mkdir files

As you can see, a new directory files/ is created.

$ ls -l

If you don’t want to create a directory in your current working directory, you can of course navigate to the parent directory (inside where you want to create the new directory) and create a new directory as shown above. But there is a better solution.

To create a new directory without navigating to the parent directory, you can specify the directory path as follows:

$ sudo mkdir /mnt/iso

This command creates a new directory iso/ inside the /mnt directory. I didn’t have to navigate to the /mnt directory as you can see.

$ ls -l /mnt

Creating a Directory along with Parent Directories with mkdir:

In the earlier section, I showed you how to create a new directory using the absolute path. But, if the parent directory does not exist, then you won’t be able to create a new directory this way. mkdir would throw an error. You will have to create the parent directory first and then create the desired directory.

Luckly, mkdir has a -p or –parents flag which will create the necessary parent directories if they do not exist.

For example, let’s say, you want to move the MySQL data directory to /db/mysql/data. But none of the parent directories exist at the moment. So, instead of using 3 mkdir command, you can run a single command to tell mkdir to create the parent directories as needed.

$ sudo mkdir -p /db/mysql/data

Or

$ sudo mkdir –parents /db/mysql/data

As you can see, the parent directories are created as required.

The mkdir with the -o flag can also create parent directories automatically for relative paths as well.

For example, let’s say, you want to create a new directory projects/python-test in your current working directory. To do that, run mkdir as follows:

$ mkdir -p projects/python-test

As you can see, the projects/python-test directory is created inside the current working directory.

Creating Multiple Directories with mkdir:

You can create multiple directories with a single mkdir command if you want.

Let’s say, you want to create 3 directories nodejs/, python/, java/ inside your current working directory ~/projects. To create all these directories, run mkdir as follows:

$ mkdir nodejs python java

As you can see, the required directories are created.

Again, let’s say, you want to create multiple directories for the mysql, mariadb, mongodb databases using absolute path. You also want to create any parent directories as required. To do that, run mkdir command as follows:

$ sudo mkdir -p /db/{mysql,mariadb,mongodb}/data

As you can see, the required directories are created all at once.

Here, the directory paths are /db/mysql/data, /db/mariadb/data, /db/mongodb/data. What changes in each directory path? Well, the second level directory mysql, mariadb, mongodb. So, we can put it inside curly braces without any whitespace as /db/{mysql,mariadb,mongodb}/data. Pretty simple.

Verbose Mode of mkdir:

By default, mkdir does not print any message on the screen if the command runs successfully. Only error messages are printed. If you want mkdir to print messages on the screen, then you can use the -v flag or the –verbose flag.

The same command in the earlier section can be written as follows as well.

$ sudo mkdir -pv /db/{mysql,mariadb,mongodb}/data

Or,

$ sudo mkdir –parents –verbose /db/{mysql,mariadb,mongodb}/data

As you can see, mkdir now prints message as directories are created.

So, that’s how you use the mkdir command in Linux. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Learn Bash Programming

Hi there. Are you new to the world of linux and trying to get around the shell? You want to become more effective hacking...
29/12/2020

Hướng dẫn sử dụng Proxy Helper Fakeip khi thuê proxy

Theo mặc định, Chrome sử dụng cài đặt proxy hệ thống. Nhưng đôi khi bạn muốn chỉ đặt proxy...
21/11/2022

30 bash script Interview questions and answers

Bash scripting is a very useful and powerful programming language that is mainly used to make any manual task automated....
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024