linux 创建目录命令_如何使用一个Linux命令创建多个子目录

linux 创建目录命令

linux 创建目录命令

00_lead_image_creating_subdirectories_wih_one_command

If you want to create a directory containing several subdirectories, or a directory tree, using the command line in Linux, generally you have to use the mkdir command several times. However, there is a faster way to do this.

如果要使用Linux中的命令行创建包含多个子目录的目录或目录树,通常必须多次使用mkdir命令。 但是,有一种更快的方法可以做到这一点。

Let’s say we’ve created a directory called htg, and want to create four subdirectories within it. In a normal situation, we’d use the mkdir command to create the htg directory. Then, we’d need the cd command to change to the new htg directory and, finally, we we’d use the mkdir command again four times to create the four subdirectories.

假设我们已经创建了一个名为htg的目录,并希望在其中创建四个子目录。 在正常情况下,我们将使用mkdir命令创建htg目录。 然后,我们需要cd命令更改为新的htg目录,最后,我们将再次使用mkdir命令四次以创建四个子目录。

01_creating_directories_with_multiple_commands

This can all be combined into one command, and we’ll show you how.

所有这些都可以组合成一个命令,我们将向您展示如何进行。

To create a new directory with multiple subdirectories you only need to type the following command at the prompt and press Enter (obviously, change the directory names to what you want).

要创建具有多个子目录的新目录,只需在提示符下键入以下命令,然后按Enter键(显然,将目录名更改为所需的目录名)。

mkdir -p htg/{articles,images,note,done}

The -p flag tells the mkdir command to create the main directory first if it doesn’t already exist (htg, in our case). The words in the brackets are part of the “brace expansion list”. Each of the items in the brace expansion list is appended separately to the preceding path (htg/).

-p标志告诉mkdir命令首先创建主目录(如果尚不存在)(在本例中为htg)。 方括号中的词是“括号扩展列表”的一部分。 大括号扩展列表中的每个项目都分别附加到前面的路径(htg /)。

02_creating_directory_tree

For example, the above command is expanded into htg/articles, htg/images, htg/note, htg/done, all four of the subdirectories being created under the htg directory. As you can see in the screenshot below, it worked.

例如,以上命令被扩展为htg / articles,htg / images,htg / note,htg / done,所有四个子目录都在htg目录下创建。 正如您在下面的屏幕快照中看到的那样,它起作用了。

03_subdirectories_created

You can also use a brace expansion list in the mkdir command if you’re creating subdirectories in a directory that already exists, as shown below. In this example, the htg directory already exists so the subdirectories are simply added under that directory.

如果要在已经存在的目录中创建子目录,也可以在mkdir命令中使用大括号扩展列表,如下所示。 在此示例中,htg目录已经存在,因此子目录仅添加到该目录下。

04_creating_subdirectories_in_an_existing_directory

You can also nest brace expansion lists in the mkdir command. For example, in the articles subdirectory under the htg directory, we want to create two subdirectories called new and rewritten. So, we type the following command at the prompt and press Enter.

您还可以在mkdir命令中嵌套括号扩展列表。 例如,在htg目录下的articles子目录中,我们想要创建两个名为new和rewrite的子目录。 因此,我们在提示符下键入以下命令,然后按Enter。

mkdir -p htg/{articles/{new,rewrites},images,notes,done}

You can also use the full path if you want, as I’ve done in the example below:

您也可以根据需要使用完整路径,如下面的示例所示:

mkdir -p ~/Documents/htg/{articles/{new,rewrites},images,notes,done}

The four subdirectories are created under the htg directory and then the two subdirectories, new and rewrites, are created under the articles subdirectory.

在htg目录下创建了四个子目录,然后在articles子目录下创建了两个子目录new和rewrites。

05_nesting_brace_expansion_list

It’s that easy. You can also combine the mkdir command with the cd command to make a directory and change to it with one command.

就这么简单。 您还可以将mkdir命令与cd命令结合使用,以创建目录并使用一个命令将其更改为目录

翻译自: https://www.howtogeek.com/275069/how-to-create-multiple-subdirectories-with-one-linux-command/

linux 创建目录命令

  • 14
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值