Explain: {,} in cp or mv Bash Shell Commands

cp /etc/httpd/httpd.{,.bakup}

mv resume{z,}.doc

Tutorial details Difficulty Easy (rss) Root privileges No Requirements None Time 2m

{}

ADVERTISEMENTS



{,} in cp or mv Bash Shell Commands

Brace expansion is a mechanism by which arbitrary strings may be generated. This mechanism is similar to filename expansion, but the file names generated need not exist. Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a seqeunce expression between a pair of braces, followed by an optional postscript. The preamble is prefixed to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right.

I often see commands as follows posted on blog or forumsORWhat is the purpose of {,} in Linux and Unix shell commands?is nothing but. From the GNU/bash man page

Open terminal and type the following command:

echo foo { 1 , 2 , 3 } .txt echo foo{1,2,3}.txt

Sample outputs:

foo1.txt foo2.txt foo3.txt

Try the following additonal examples to create arguments for commands and save typing time:

### I am using echo for demo purpose only #### echo file.txt { ,.bak } echo file- { a..d } .txt echo mkdir -p / apache-jail / { usr,bin,lib64,dev } echo cp httpd.conf { ,.backup } echo mv delta. { txt,doc } ### I am using echo for demo purpose only #### echo file.txt{,.bak} echo file-{a..d}.txt echo mkdir -p /apache-jail/{usr,bin,lib64,dev} echo cp httpd.conf{,.backup} echo mv delta.{txt,doc}

Examples

You can use brace expansion to copy file, rename/backup file, or create directories. In this traditional example, make a backup of a file named file1.txt to file1.txt.bak, type:

cp -v file1.txt file1.txt.bak cp -v file1.txt file1.txt.bak

You can save time with brace expansion as follows when using the cp command:

cp -v file1.txt { ,.bak } cp -v file1.txt{,.bak}

Sample outputs:

file1.txt -> file1.txt.bak

More brace expansion examples

Try the following examples. Want to list all pdf and png files from Downloads and Pictures folders on Linux? Try:

ls -l ~/{Downloads,Pictures}/*.{pdf,png}

Brace expansions may be nested on Linux and Unix systems. For example:

echo a { 1 , 2 , 3 } b echo a { 1 , 2 } { b,c } echo 'Hi ' { Wends,Marlena,Vivek,Raj } $ ', let\' s be friends !

' sudo chown www:data /wwwdata/{images/{old,new},lib/{how_to?.?*,old_html}} echo a{1,2,3}b echo a{1,2}{b,c} echo 'Hi '{Wends,Marlena,Vivek,Raj}$', let\'s be friends!

' sudo chown www:data /wwwdata/{images/{old,new},lib/{how_to?.?*,old_html}}

Putting it all together

Let us use bash for loop to update systems using the apt command or yum command:

## update all CentOS/ RHEL 7.x boxes named ## for server in aws- { prod,backup-prod } - { db,www } - 0 { 1 .. 4 } do ssh -t vivek @ ${server} sudo -- sh -c 'yum update' done ## update all CentOS/ RHEL 7.x boxes named ## for server in aws-{prod,backup-prod}-{db,www}-0{1..4} do ssh -t vivek@${server} sudo -- sh -c 'yum update' done

See “sudo: Sorry, you must have a tty to run sudo Error on a Linux and Unix” and “How to run multiple commands in sudo under Linux or Unix” for more info.

Conclusion

You learned how to use brace�expansion�to create arbitrary strings when using bash or sh including {,} in cp or mv Bash. It is similar to�pathname expansion, but the filenames generated need not exist.

See bash(1) command man page for more information.