linux中mkdir命令_Linux / Unix中的mkdir命令

linux中mkdir命令

The Linux directory structure is such that at any given point in the terminal, you are working inside a particular directory. The Linux directory hierarchy starts at the top with the root (/) directory and branches into several subdirectories as you go down the hierarchy tree.

Linux目录结构使您可以在终端中的任何给定位置使用特定目录。 Linux目录层次结构从根(/)目录的顶部开始,并在您进入层次结构树时分支到几个子目录中。

In this guide, we will focus on mkdir command. mkdir – short for make directory – is a command used for creating directories in Linux/Unix systems and assign other attributes as well.

在本指南中,我们将重点介绍mkdir命令。 mkdir -短make目录 -用于创建在Linux / Unix系统,并指定其他属性以及目录的命令。

使用mkdir命令创建目录 (Create a directory using the mkdir command)

To create a directory using the mkdir command use the syntax as shown below

要使用mkdir命令创建目录,请使用如下所示的语法

Syntax

句法

mkdir [OPTIONS] directory_name

To find out your current working directory run:

要查找您当前的工作目录,请运行:

pwd

In my case, this happens to be /home/jamie as shown in the following image.

就我而言,这恰好是/home/jamie ,如下图所示。

Now we going to navigate to the ‘Documents’ directory

现在,我们将导航到“文档”目录

And create 3 directories – directory1, directory2, and directory3.

并创建3个目录-directory1,directory2和directory3。

The syntax for creating directories without arguments using the mkdir command is:

使用mkdir命令创建不带参数的目录的语法是:

mkdir directory_name

Therefore to create all the three directories, we are going to run:

因此,要创建所有三个目录,我们将运行:

mkdir directory1 directory2 directory3

You can verify this using the ls -l command.

您可以使用ls -l命令进行验证。

使用-v选项打印或显示详细输出 (Print or Display verbose output using -v option)

If you desire to print or display the operation of mkdir command, use the -v option as shown below.

如果要打印或显示mkdir命令的操作,请使用-v选项,如下所示。

mkdir -v directory_name

In our case, we shall create 3 more directories and display the output.

在本例中,我们将再创建3个目录并显示输出。

mkdir -v directory4 directory5 directory6

Output

输出量

As shown above, the verbose output has been printed showing the operations that have been carried out.

如上所示,已打印详细输出,显示已执行的操作。

使用-p选项创建子目录 (Create subdirectories using the -p option)

The mkdir -p command allows you to create nested directories or parent directories only if they do not exist.

mkdir -p命令允许您仅在嵌套目录或父目录不存在时创建它们

When the above command is repeated, nothing will happen and no error will be reported. It is thus said to be an idempotent operation.

重复上述命令后,将不会发生任何事情,也不会报告任何错误。 因此,这被称为幂等运算。

If you have a directory “/dir1” and you run following command:

如果您有目录“ / dir1”,并且运行以下命令:

mkdir -p /dir1/dir2/dir3

The above command creates dir2 inside dir1 and dir3 in dir2.

上面的命令在dir1内部创建dir2,在dir2中创建dir3。

Example

mkdir -p linux/ditros/debian

This creates “debian” directory inside “distros” folder and “distros” inside “linux” directory.

这将在“ distros”文件夹内创建“ debian”目录,并在“ linux”目录内创建“ distros”。

This can be confirmed by navigating to the debian/distros/debian path and running pwd command.

可以通过导航到debian / distros / debian路径并运行pwd命令来确认。

Confirming the current working directory with pwd command.

使用pwd命令确认当前工作目录。

使用-m选项分配权限 (Assigning permissions using the -m option)

By default , the mkdir assigns rwx r_x r_x permission or simpy 755 in Octal format. If you want to assign different values use the mkdir -m and the corresponding permissions.

默认情况下, mkdir以八进制格式分配rwx r_x r_x权限或simpy 755。 如果要分配不同的值,请使用mkdir -m和相应的权限。

Syntax

句法

mkdir -m  octal_value directory_name

For example, we are going to create a directory called docs and assign it all the permissions (octal value 777).
To accomplish that, we shall execute the following command.

例如,我们将创建一个名为docs的目录并为其分配所有权限(八进制值777)。
为此,我们将执行以下命令。

mkdir -m 777 docs

To verify that we have created the directory with the assigned values execute:

要验证我们是否已创建具有分配值的目录,请执行以下命令:

ls -l

Output

mkdir command with permissions

输出量

As seen above the docs directory has all the permissions assigned to it and is highlighted in green.

如上所示, docs目录已分配了所有权限,并以绿色突出显示。

访问mkdir命令的联机帮助页 (Accessing manpages for mkdir command)

To access mkdir command man pages and learn more about the command usage, execute the command below.

要访问mkdir命令手册页并了解有关命令用法的更多信息,请执行以下命令。

man mkdir

Output

输出量

获取mkdir命令的版本 (Getting the version for mkdir command)

To obtain the version of mkdir command in your system execute:

要获取系统中mkdir命令的版本,请执行:

mkdir --version

Output

输出量

结论 (Conclusion)

In this guide, we dissected the mkdir command and gave example usages of the command. We do hope that you have found this useful especially if you are a beginner and learning the ropes in Linux. Feel free to try out the commands and get back to us. Your feedback is most welcome.

在本指南中,我们剖析了mkdir命令并给出了该命令的示例用法。 我们确实希望您发现这很有用,特别是对于初学者并学习Linux的绳索。 随意尝试这些命令并返回给我们。 非常欢迎您提供反馈。

翻译自: https://www.journaldev.com/24544/mkdir-command-linux-unix

linux中mkdir命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值