微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐