Bash 脚本创建多个目录和子目录

lud*_*rZA 4 scripts

我目前正在使用以下两个脚本来创建多个目录和子目录。第一个脚本创建“/Dir1/Dir2”,然后第二个脚本将多个子目录添加到“Dir2”。

#!/bin/bash
# Creates directories and subdirectories from text file
cat *.txt | xargs -L 1 mkdir -p 

#!/bin/bash
# Lists all directories in current working directory and create subdirectories 
LIST=`ls -D`
for i in $LIST;
do
mkdir -p $i"/Dir3/Dir4/"
done
Run Code Online (Sandbox Code Playgroud)

我已经组合了这两个脚本,但是子目录 Dir3 和 Dir4 是在 Dir1 中创建的,而不是按预期在 Dir2 中创建的。由于我对脚本的了解非常有限,我无法成功搜索答案,因此向你们寻求帮助。

Ale*_* L. 7

很难说出您要做什么,但我认为这可以满足您的需求:

#!/bin/bash

cat *.txt | while IFS= read -r line; do
    mkdir -p -- "$line/Dir3/Dir4"
done
Run Code Online (Sandbox Code Playgroud)

这将通读.txt当前目录中的所有文件,并使用路径创建一个文件夹./<line from file>/dir3/dir4

如果这是您想要发生的事情,那么它不起作用的原因是它$(ls -D)只会列出当前目录中的文件。它不会向下递归到子目录。因此,即使第一个脚本创建了一个dir1/dir2目录,第二个脚本也只能看到该dir1目录。