所以我刚刚了解了 bash 扩展...这很酷:)现在我想为明年制定一些好的计划(仅作为示例)。
我不知道如何创建文件夹并一次性在每个文件夹中创建一些文件...这可能吗,或者我需要进入每个目录并手动重复所有内容?
sean@COMMODORE:~$ mkdir plans && cd plans && mkdir 2021-CW{01..52}
sean@COMMODORE:~/plans$ ls
2021-CW01 2021-CW05 2021-CW09 2021-CW13 2021-CW17 2021-CW21 2021-CW25 2021-CW29 2021-CW33 2021-CW37 2021-CW41 2021-CW45 2021-CW49
2021-CW02 2021-CW06 2021-CW10 2021-CW14 2021-CW18 2021-CW22 2021-CW26 2021-CW30 2021-CW34 2021-CW38 2021-CW42 2021-CW46 2021-CW50
2021-CW03 2021-CW07 2021-CW11 2021-CW15 2021-CW19 2021-CW23 2021-CW27 2021-CW31 2021-CW35 2021-CW39 2021-CW43 2021-CW47 2021-CW51
2021-CW04 2021-CW08 2021-CW12 2021-CW16 2021-CW20 2021-CW24 2021-CW28 2021-CW32 2021-CW36 2021-CW40 2021-CW44 2021-CW48 2021-CW52
sean@COMMODORE:~/plans$ cd 2021-CW01
sean@COMMODORE:~/plans/2021-CW01$ touch {work,sport,shopping,music}.txt
sean@COMMODORE:~/plans/2021-CW01$ ls
music.txt shopping.txt sport.txt work.txt
sean@COMMODORE:~/plans/2021-CW01$
答案1
您也可以将第一个命令简化mkdir
为一个命令-p
来创建父目录:
mkdir -p plans/2021-CW{01..52}
然后只使用一个touch
在所有目录中创建文件
touch plans/2021-CW{01..52}/{work,sport,shopping,music}.txt
$ ls plans/2021-CW*
plans/2021-CW01:
music.txt shopping.txt sport.txt work.txt
plans/2021-CW02:
music.txt shopping.txt sport.txt work.txt
...
plans/2021-CW52:
music.txt shopping.txt sport.txt work.txt
答案2
我编写了一个脚本,您可以使用它来执行此操作:
#!/bin/bash
mkdir -p plans/2021-CW{01..52} && cd plans
for d in */
do
if [ -d "${d}" ] ; then
cd ${d} ; touch {work,sport,shopping,music}.txt ; cd ..
fi
done