mkdir -p cmd 未找到

问题描述 投票:0回答:1

我基本上有一个包含我想要创建的所有目录的循环。在循环内有相同单词的重复,我想在此路径中的每个单词创建一个目录,所有目录都放在更大的目录下。

代码基本上是这样的

directory_path=一/一/一/二/三/三/四/五 它打印出来就像 一 一 一 二 等等..

当我尝试 mkdir $serverdata 时,问题就开始了 mkdir -p $directory_path>$serverdata

有什么想法为什么或者我可以做什么吗?

linux bash mkdir
1个回答
0
投票

首先,您需要将字符串拆分为单个单词,因此最好使用

IFS
(内部字段分隔符)变量来实现此目的,并且
mkdir
不接受来自标准输入 (
>
) 的输入,因此您可以使用循环一一创建目录。

因此,在我的场景中,

directory_path
中的每个单词都会创建为
base_directory
下的目录,您可以将
/path/to/base_directory
替换为您要创建目录的实际路径,请查看:

directory_path="one/one/one/two/three/three/four/five"

#set the base directory where all the directories will be created
base_directory="/path/to/base_directory"

#set the IFS to split the string into words
IFS="/"

#split the string into an array of words
read -ra words <<< "$directory_path"

#creating each directory one by one
for word in "${words[@]}"; do
  #creating the directory under the base directory
  mkdir -p "$base_directory/$word"
done
© www.soinside.com 2019 - 2024. All rights reserved.