Wednesday, February 18, 2009

Shell braces

most shells with expand braces with commas, like so:

$ echo file{1,2,a}
file1 file2 filea

You can even nest them:

$ echo file{1,2,3{1,2}}
file1 file2 file31 file32

There are several cool uses for this trick.

The first to copy a file to itself plus .bak:

cp /path/to/somefile{,.bak}

You can also combine this with backtick to version a file with a date:

cp /path/to/somefile{,-`date -I`}

No comments: