bash - 对于文件夹中的每个目录,在不同的位置创建一个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12622415/
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 03:24:00  来源:igfitidea点击:

bash - For each directory in a folder, make a directory within a different location

bash

提问by RafaelGP

I have a directory which contains several folders. I'd like to make a bash script to create directories with the same name in a different location.

我有一个包含多个文件夹的目录。我想制作一个 bash 脚本来在不同的位置创建具有相同名称的目录。

Note that I don't want to copy the folder structure, neither copy or move the folders. I just want to create folders with the same name in a different location.

请注意,我不想复制文件夹结构,也不想复制或移动文件夹。我只想在不同的位置创建具有相同名称的文件夹。

I'm stuck. Using this:

我被困住了。使用这个:

for d in "template/modules/*"
do
    # mkdir $(basename "$d");
    echo $d;
    echo "$d";
    echo $(basename "$d");
done

Output:

输出:

template/modules/introduction template/modules/kitchen template/modules/test
template/modules/*
add_module.sh template

Any ideas?

有任何想法吗?

Cheers

干杯

回答by l0b0

You need to put the asterisk outside the quotes:

您需要将星号放在引号之外:

for d in "template/modules/"*

In the first echo, $dis expanded, but when you quote it, it's not.

在第一个echo,$dexpand,但是当你引用它时,它不是。

回答by dogbane

To copy the directory structure, you can do this:

要复制目录结构,您可以这样做:

$ cd template/modules
$ find . -type d -maxdepth 1 -exec mkdir -p ../../{} \;

回答by turtledove

try this:

尝试这个:

for d in "template/modules/*"
do
   mkdir $(basename "$d")
done