Ever needed to quickly backup a file before editing it? Most developers do this the tedious way by typing the filename twice. There’s a much smarter approach that can save you time and reduce typing errors.
The Problem
You’re about to modify an important configuration file and want to create a backup first. The traditional approach looks like this:
cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backupNotice how you had to type that long path twice? It’s tedious, error-prone, and wastes time. What if there was a better way?
The Hack: Brace Expansion
Bash has a powerful feature called brace expansion that lets you generate multiple strings from a pattern. Here’s the elegant solution:
cp /etc/apache2/apache2.conf{,.backup}That’s it! The curly braces with a comma inside tell Bash to expand this into two arguments: the original filename and the filename with .backup appended.
How It Works
When Bash sees {,.backup}, it expands it into two strings:
- First string: empty (nothing before the comma)
- Second string: .backup
So the command becomes:
cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backupPractical Examples
Backup with Timestamp
cp config.yaml{,.$(date +%Y%m%d)}This creates: config.yaml.20251024
Restore from Backup
cp database.sql{.backup,}Notice the order is reversed – backup comes first, then empty string, restoring the original file.
Compare Files
diff nginx.conf{.old,}Compare the old version with the current one in a single command.
Advanced Brace Expansion Techniques
Multiple Backups
cp important.txt{,.bak1,.bak2,.bak3}Creates three backup copies at once!
Create Directory Structure
mkdir -p project/{src,tests,docs,config}Creates four directories in one command.
Rename with Pattern
mv photo.{jpg,png}Renames photo.jpg to photo.png.
Work with Sequences
# Create numbered files
touch file{1..10}.txt
# Create monthly reports
mkdir report-2025-{01..12}
# Generate alphabet
echo {A..Z}
# With padding
echo {01..31}Real-World Use Case
Imagine you’re a system administrator about to update a critical configuration file on a production server. Here’s your workflow:
# Backup before editing
sudo cp /etc/nginx/nginx.conf{,.backup-$(date +%Y%m%d-%H%M%S)}
# Edit the file
sudo vim /etc/nginx/nginx.conf
# Test the configuration
sudo nginx -t
# If something breaks, restore quickly
sudo cp /etc/nginx/nginx.conf{.backup-20251024-140530,}
# Or compare changes
diff /etc/nginx/nginx.conf{.backup-20251024-140530,}Pro Tips
- No Spaces: Don’t put spaces inside the braces or around commas
- Combine with Commands: Works with any command that accepts multiple arguments
- Nested Expansion: You can nest brace expansions for complex patterns
- Shell Agnostic: Works in Bash, Zsh, and most modern shells
Common Mistakes to Avoid
# Wrong - space after comma
cp file.txt{, .backup}
# Wrong - space inside braces
cp file.txt{ ,.backup }
# Correct
cp file.txt{,.backup}Why This Matters
This simple hack saves you:
- Time typing long paths
- Potential typos when retyping filenames
- Mental overhead of remembering exact paths
- Frustration when dealing with deeply nested directories
For a developer who backs up files dozens of times a day, this can save hours over a year. More importantly, it reduces the cognitive load of routine tasks, letting you focus on actual problem-solving.
Conclusion
Brace expansion is one of those simple shell features that, once you start using it, you’ll wonder how you ever lived without it. It’s elegant, efficient, and reduces the friction in your daily workflow.
Start using it today, and watch your productivity soar!
