The shell (in this case bash) is packed with features. So much so that you will probably never ever learn them all. Brace expansion is one of those things.

What is it? In the bash manual it says:

Brace Expansion

Brace expansion is a mechanism by which arbitrary strings may be generated. This mechanism is similar to pathname expansion, but the filenames generated need not exist

So what is it? You can make your shell generate strings, like so:

$ echo {one,two}
one two

Or, somewhat more useful

$ echo prefix_{one,two}
prefix_one prefix_two


$ echo prefix{,two}
prefix prefixtwo


$ echo {1..4}
1 2 3 4

Think of it as sort of a pathname expansion, but the filenames do not have to exist.

More down to the earth examples include: moving a file to the same name, but then with .bak added.

Cumbersome way:

$ mv mylongfilename mylongfilename.bak


$ mv mylongfilename{,.bak}

Which first generates nothing, and leaves mylongfilename and a mylongfilename with .bak appended.

With this you can create neat little scripts, like the following one, which I use for archiving my E-mail. I’m especially fond of the

rmdir ~/Mailback/$BCK/{old,sent}/{new,tmp}

CODE(sh){ #!/bin/bash

Archive Maildir format

Move the directory ~/Maildir/.old/* to ~/Mailback/YYYY-MMM

crawl the directories and bzip2 everything

BCK="$(date +%Y)-$(date +%b)" mkdir -p ~/Mailback/$BCK/{old,sent}

mv ~/Maildir/.old/* ~/Mailback/$BCK/old
mv ~/Maildir/.sent/* ~/Mailback/$BCK/sent rmdir ~/Maildir/.{old,sent} # mutt will recreate them

new and tmp should be empty

rmdir ~/Mailback/$BCK/{old,sent}/{new,tmp}

now bzip2’em

( cd ~/Mailback/$BCK/old/cur; ls | xargs bzip2 -7 ) ( cd ~/Mailback/$BCK/sent/cur; ls | xargs bzip2 -7 ) }CODE