bash 脚本在目录中创建多个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10296172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:07:26  来源:igfitidea点击:

bash script create multiple directories within directories

bash

提问by ibash

I want to create a directory structure like this in nix:

我想在 nix 中创建这样的目录结构:

mkdir -p 1,2,3,4,5,6,7,8,9and within each of these folders I want folders 1,2,3,4,5,6,7,8,9

mkdir -p 1,2,3,4,5,6,7,8,9在这些文件夹中的每一个中,我想要文件夹 1,2,3,4,5,6,7,8,9

I have started to write a simple loop like this (all the way up to folder 2) but this seems inefficient.

我已经开始写一个像这样的简单循环(一直到文件夹 2),但这似乎效率低下。

#!/usr/bin/env bash   
for i in 1 2 4 5 6 7 8 9; do mkdir -p 1/{1,2,3,4,5,6,7,8,9} $i, mkdir -p  2/{1,2,3,4,5,6,7,8,9} ; done

Is there a better way of doing it?

有更好的方法吗?

回答by Micha? Kosmulski

This should help (requires bash):

这应该会有所帮助(需要 bash):

mkdir -p {1,2,3,4,5,6,7,8,9}/{1,2,3,4,5,6,7,8,9}

Some newer versions of bash also allow this:

一些较新版本的 bash 也允许这样做:

mkdir -p {1..9}/{1..9}

回答by David Kennedy

Sounds simple enough unless I've misunderstood:

除非我误解,否则听起来很简单:

#!/bin/sh
for i in `seq 1 9`; do
  for j in `seq 1 9`; do
    mkdir -p $i/$j
  done
done

回答by Debaditya

Perl Solution.

Perl 解决方案。

for($counter = 1; $counter <= 9; $counter++)
{
        `mkdir -p $counter/{1..9}`; //Executing Unix Command
}