Linux mkdir 命令的初学者教程
mkdir 目录
![](/data/attachment/album/201802/17/124700v1sssrbsovwoe18y.jpg)


当你使用命令行的时候，无论什么时候，你都位于一个目录中，它告诉了命令行当前所位于的完整目录。在 Linux 中，你可以使用 [rm](/article-9321-1.html) 命令删除目录，但是首先，你需要使用 `mkdir` 命令来创建目录。在这篇教程中，我将使用一些易于理解的例子来讲解这个工具的基本用法。


在开始之前，值得一提的是，这篇教程中的所有例子都已经在 Ubuntu 16.04 LTS 中测试过。


### Linux mkdir 命令


正如上面所提到的，用户可以使用 `mkdir` 命令来创建目录。它的语法如下：



```
mkdir [OPTION]... DIRECTORY...

```

下面的内容是 man 手册对这个工具的描述：



> 
> 如果目录还不存在，则创建之。
> 
> 
> 


下面这些 Q&A 式的例子将能够帮助你更好的理解 `mkdir` 这个命令是如何工作的。


### Q1. 如何使用 `mkdir` 命令创建目录？


创建目录非常简单，你唯一需要做的就是把你想创建的目录的名字跟在 `mkdir` 命令的后面作为参数。



```
mkdir [dir-name]

```

下面是一个简单例子：



```
mkdir test-dir

```

### Q2. 如何确保当父目录不存在的时候，同时创建父目录？


有时候，我们需要使用一条 `mkdir` 命令来创建一个完整的目录结构，这时候，你只需要使用 `-p` 这个命令行选项即可。


比如，你想创建目录 `dir1/dir2/dir3`，但是，该目录的父目录都不存在，这时候，你可以像下面这样做：



```
mkdir -p dir1/dir2/dir3

```

[![How to make sure parent directories (if non-existent) are created](/data/attachment/album/201802/17/124712ke4vbazwtebta4eb.png)](https://www.howtoforge.com/images/command-tutorial/big/mkdir-p.png)


### Q3. 如何在创建目录时自定义权限？


默认情况下，`mkdir` 命令创建目录时会把权限设置为 `rwx, rwx, r-x` 。


[![How to set permissions for directory being created](/data/attachment/album/201802/17/124715j7jd9jt9jzc95vth.png)](https://www.howtoforge.com/images/command-tutorial/big/mkdir-def-perm.png)


但是，如果你想自定义权限，那么你可以使用 `-m` 这一命令行选项。


[![mkdir -m command option](/data/attachment/album/201802/17/124716pqzvq22qyxqqytcq.png)](https://www.howtoforge.com/images/command-tutorial/big/mkdir-custom-perm.png)


### Q4. 如何使 `mkdir` 命令显示操作细节？


如果你希望 `mkdir` 命令显示它所执行的操作的完整细节，那么你可以使用 `-v` 这一命令行选项。



```
mkdir -v [dir]

```

下面是一个例子：


[![How to make mkdir emit details of operation](/data/attachment/album/201802/17/124719w9sdmccbhmbuw2hv.png)](https://www.howtoforge.com/images/command-tutorial/big/mkdir-verbose.png)


### 结论


你已经看到，`mkdir` 是一个非常简单，易于理解和使用的命令。学习这一命令不会遇到任何屏障。在这篇教程中，我们讨论到了它的绝大部分命令行选项。记得练习这些命令，并在日复一日的工作中使用这些命令。如果你想了解关于这一命令的更过内容，请查看它的 [man](https://linux.die.net/man/1/mkdir) 手册。




---


via: <https://www.howtoforge.com/linux-mkdir-command/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[ucasFL](https://github.com/ucasFL) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux mkdir Command Explained for Beginners (with examples)

At any given time on the command line, you are in a directory. So it speaks for itself how integral directories are to the command line. In Linux, while the rm command lets you delete directories, it's the **mkdir** command that allows you create them in the first place. In this tutorial, we will discuss the basics of this tool using some easy to understand examples.

But before we do that, it's worth mentioning that all examples in this tutorial have been tested on Ubuntu 16.04 LTS.

## Linux mkdir command

As already mentioned, the mkdir command allows the user to create directories. Following is its syntax:

mkdir [OPTION]... DIRECTORY...

And here's how the tool's man page describes it:

Create the DIRECTORY(ies), if they do not already exist.

The following Q&A-styled examples should give you a better idea on how mkdir works.

## Q1. How to create directories using mkdir?

Creating directories is pretty simple, all you need to do is to pass the name of the directory you want to create to the mkdir command.

mkdir [dir-name]

Following is an example:

mkdir test-dir

## Q2. How to make sure parent directories (if non-existent) are created in process?

Sometimes the requirement is to create a complete directory structure with a single mkdir command. This is possible, but you'll have to use the **-p** command line option.

For example, if you want to create dir1/dir2/dir2 when none of these directories are already existing, then you can do this in the following way:

mkdir -p dir1/dir2/dir3

## Q3. How to set permissions for directory being created?

By default, the mkdir command sets rwx, rwx, and r-x permissions for the directories created through it.

However, if you want, you can set custom permissions using the **-m** command line option.

## Q4. How to make mkdir emit details of operation?

In case you want mkdir to display complete details of the operation it's performing, then this can be done through the **-v** command line option.

mkdir -v [dir]

Here's an example:

## Conclusion

So you can see mkdir is a pretty simple command to understand and use. It doesn't have any learning curve associated with it. We have covered almost all of its command line options here. Just practice them and you can start using the command in your day-to-day work. In case you want to know more about the tool, head to its [man page](https://linux.die.net/man/1/mkdir).