Using Bash brace expansion to generate multiple files

I needed to generate a whole bunch of files, with identical content, for a recent task. You might automatically think of using a loop for such a task but there’s a much simpler method using brace expansion in the shell.

I wanted to generate files in the following format…

rhys-tmp01.txt
rhys-tmp02.txt
rhys-tmp03.txt
...
rhys-tmp91..txt
rhys-tmp92.txt

This is achievable with a simple one-liner once we have created the source file rhys-tmp01.txt:

tee rhys-tmp{02..92}.txt < rhys-tmp01.txt

Note that this uses zero-padding and this won't work in old versions of bash (probably needs to be at least version 4).

Leave a Reply