我使用 mkdir 创建了一个父文件夹,其中包含另外 10 个子文件夹mkdir -p 父\子{1..10}。我现在需要在这 10 个子文件夹中的每一个中创建一个 txt 文件作为(父文件夹的名称).txt
我无法使用触摸命令,因为每个文本文档也需要有一行文本。
我最初的想法是创建一个循环,使每个子文件夹和 txt 文件分别具有指定的文本行 10 次。我认为这效率很低。
我可以使用一个命令创建所有 10 个子文件夹和父文件夹。我将如何使用文本行以有效的方式创建文本文件?或者我最初的想法是实现它的最佳方式吗?
答案1
其中一种方法可能如下所示:
mkdir -p parent/child{1..10}
for d in parent/child{1..10}; do echo "This file is stored in ${d##*/}" > "$d/parent.txt"; done
查看结果:
$ head parent/child*/parent.txt
==> parent/child10/parent.txt <==
This file is stored in child10
==> parent/child1/parent.txt <==
This file is stored in child1
==> parent/child2/parent.txt <==
This file is stored in child2
==> parent/child3/parent.txt <==
This file is stored in child3
==> parent/child4/parent.txt <==
This file is stored in child4
==> parent/child5/parent.txt <==
This file is stored in child5
==> parent/child6/parent.txt <==
This file is stored in child6
==> parent/child7/parent.txt <==
This file is stored in child7
==> parent/child8/parent.txt <==
This file is stored in child8
==> parent/child9/parent.txt <==
This file is stored in child9