bash 在现有子目录中递归创建目录树

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

Recursively create directory tree within existing subdirectories

linuxbashshell

提问by ap123

I have a working directory with a large number of subfolders (i.e 1190A, 1993A etc).

我有一个包含大量子文件夹(即 1190A、1993A 等)的工作目录。

'/working/1190A'
'/working/1993A'

I would like to recursively create a certain directory tree within each subfolder. For example:

我想在每个子文件夹中递归地创建一个特定的目录树。例如:

'/working/1190A/analysis/1'
'/working/1993A/analysis/1'
etc

Thanks.

谢谢。

回答by fedorqui 'SO stop harming'

To force the system create a directory tree without having to create each level of it, add -pto the mkdircommand.

要强制系统创建目录树而不必创建它的每个级别,请添加-pmkdir命令中。

Hence, this could work:

因此,这可以工作:

for dir in list_of_folders
do
   mkdir -p $dir/your/directory/tree
   [ $? ] && echo "error on $dir" # if the dir could not be created, print error (thanks @hetepeperfan - see comments)
done

Note that the list_of_folderscan be given like /working/1190A /working/1993A, but also generated with a findcommand. This is just a first version you'd better adapt to your specific requirements.

请注意,list_of_folders可以像 一样给出/working/1190A /working/1993A,但也可以用find命令生成。这只是您最好适应您的特定要求的第一个版本。

回答by Rohan

This is one liner as

这是一个班轮

cd /working
find . -maxdepth 1 -type d -exec  mkdir -p '{}'/analysis/1 \;

回答by vpram86

yourDirList=/working/*
for f in $yourDirList
do
    if [ -d $f ]
        mkdir -p $f/analysis/1
    fi
done