CLI Knowledge Base

Brace Expansion

Examples

Rename or backup a file or folder
cp -v some_file{,~}.html
Create multiple folders
mkdir -pv text~{0..2}
Use a template to create multiple files
for f in some_file_0{1..9}.html ; do cp -v template.html $f ; done
Integer or character ranges 1
mv -v 2018_0{1..5}* 2018/
echo {a..f}.txt
String lists2
mkdir /home/bash/test/{foo,bar,baz,cat,dog}
Increment 2
{<START>..<END>..<INCR>} $ touch 2018_0{1..9..1}.txt
$ ls -1
2018_01.txt
2018_02.txt
2018_03.txt
2018_04.txt
2018_05.txt
2018_06.txt
2018_07.txt
2018_08.txt
2018_09.txt
Get last field (space delimited) 3
${var##* }

Example:

$ var='foo bar spam egg'
$ echo "${var##* }"
egg
NULL