首頁>Linux>source

所以我刚刚了解了bash擴充套件...這很酷:)現在,我想為明年製定一些好的計划(仅作為示例)。

我想不通如何建立檔案夹並在每个檔案夹中一次建立一些檔案...這可能吗,或者我需要进入每个目錄並手動重複所有操作?

[email protected]:~$ mkdir plans && cd plans && mkdir 2021-CW{01..52}
[email protected]:~/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
[email protected]:~/plans$ cd 2021-CW01
[email protected]:~/plans/2021-CW01$ touch {work,sport,shopping,music}.txt
[email protected]:~/plans/2021-CW01$ ls
music.txt  shopping.txt  sport.txt  work.txt
[email protected]:~/plans/2021-CW01$
最新回復
  • 5月前
    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
    

  • 5月前
    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
    

  • shell:什麼是" $ _"的未展開版本?
  • text processing:替換字元對之間的字元串