From Bash Pocket Reference and Advanced Bash−Scripting Guide
When you’re working with variations of a file—like backups or different file types—it can get tedious typing out the same commands with small tweaks. Using the brace symbols ({}
), you can easily perform batch operations on multiple versions of a file.
Say you want to rename just part of a filename. Instead of typing out mv /path/to/file.txt /path/to/file.xml
, you could just run:
mv /path/to/file.{txt,xml}
This runs the command with the same arguments, only with the parts inside the brace changed—the first part corresponding to the first argument, the second part corresponding to the second argument.
The most common example of this is when you’re backing up a file that you’re making changes to. For example, if you are tweaking your rc.conf
, you’ll want to make a backup in case the new one doesn’t work. So, to do so, you can just run:
sudo cp /etc/rc.conf{,-old}
Putting nothing before the comma will just append -old
to the filename after copying it with cp
. If your new file doesn’t work out and you want to restore the backed up file to its original location, you can just use:
sudo mv /etc/rc.conf{-old,}
Moving the comma to the other end of the brace will remove -old
from the end of the file and restore it to its original name.
The braces can also work when moving or creating multiple files at once. For example, if you wanted to create three numbered directories, you could just run:
mkdir myfolder{1,2,3}
This will create three folders: myfolder1, myfolder2, and myfolder3.