[Bash] Create and Copy Multiple Files with Brace Expansions in Bash

Copy

Brace expansions are commonly used with the cp or mv commands in order to dynamically generate strings. In this lesson, we'll use a brace expansion to shorten a command like cp index.js index.js.backup to cp index.js{,.backup}. We'll also see how brace expansions can generate sequences. For example, touch test-{1..10} will generate the arguments to touch to create 10 test files.

Note that the tree command that I use in this lesson has to be installed.

Case 0:

It acts like a loop

echo pre-{a,b,c}-post
## pre-a-post pre-b-post pre-c-post
echo pre-{,b,c}-post
## pre--post pre-b-post pre-c-post
echo {1..10}
## 1 2 3 4 5 6 7 8 9 10
echo {a..z}
## a b c d e f g h i j k l m n o p q r s t u v w x y z

Case 1:

copy index.js and create a backup file called index.js.backup

cp index.js{,.backup}

Case 2:

Create multi nested folder with same structure

mkdir -p packages/{pkg1,pkg2,pkg3}/src
tree

Case 3:

Greate multi test files

touch test-{1..3}.js
posted @ 2021-02-11 19:56  Zhentiw  阅读(78)  评论(0编辑  收藏  举报