初探 Linux 的大括號擴展(Brace expansion)
大括號擴展(Brace expansion)是一種機制, 協助我們使用省略的字串產生相對應的字串組合,
而且產生的字串組合, 不一定要有實際檔案的存在,
在這裡下指令 echo 做示範, 套用到 cp 指令後, 檔案 foo1.txt 就會複製到 foo2.txt
# echo foo{1,2}.txt
foo1.txt foo2.txt
# cp -v foo{1,2}.txt
'foo1.txt' -> 'foo2.txt'
大括號擴展比較常用的關鍵字有 逗號(,) 和 句號(.) ,
逗號是用來分開不同的字串, 而句號是用來產生有序列性的字串
逗號
使用範例
# echo file{,1,2}
file file1 file2
# mkdir -v file{,1,2}
mkdir: created directory 'file'
mkdir: created directory 'file1'
mkdir: created directory 'file2'
句號
使用範例
# echo file{1..5}
file1 file2 file3 file4 file5
# mkdir -v file{1..5}
mkdir: created directory 'file1'
mkdir: created directory 'file2'
mkdir: created directory 'file3'
mkdir: created directory 'file4'
mkdir: created directory 'file5'
複雜的使用方式
在一個字串中同時放很多個大括號擴展
# echo {apache,nginx}-{index,default}-0{1..2}.html
apache-index-01.html apache-index-02.html apache-default-01.html apache-default-02.html nginx-index-01.html nginx-index-02.html nginx-default-01.html nginx-default-02.html