Shell Brace Expansion

I've only just discovered brace expansion in the shell. How had I not known about this before? Somehow I've never seen someone using this clever time saving trick on the command line.

I have a subdirectory. It contains old-file.txt. I need to copy it to new-file.txt. I've spent years typing:

$ cp /subdirectory/old-file.txt /subdirectory/new-file.txt

When instead I should have been using:

$ cp /subdirectory/{old-file.txt,new-file.txt}

Or even more succinct:

$ cp /subdirectory/{old,new}-file.txt


i.  [preamble]{comma,separated,strings}[postscript]
ii. [preamble]{x..y[..incr]}[postscript]

The braces may contain a set of comma-separated strings or a sequence expression. preamble and postscript are optional.

The comma-separated sequence {aa,bb,cc} becomes aa bb cc. While the sequence expression {1..10} becomes 1 2 3 4 5 6 7 8 9 10. The sequence also takes an optional increment value. So, {1..10..2} expands to 1 3 5 7 9


How to create a backup of file.txt. Easy:

$ cp /subdirectory/file.txt{,.bak}

Create five new directories within subdirectory:

$ mkdir /subdirectory/dir_0{1..5}

Moz Morris

Moz Morris

Freelance Web Developer