Member-only story
The Fastest Way to Back Up a File in Bash (You’re Probably Not Using It)
When working in the terminal, backing up a file before editing it is a habit that saves time, nerves, and sometimes entire systems. There’s a Bash trick that makes this almost effortless:
cp filename{,.bak}How It Works
This command relies on brace expansion, a shell feature that generates multiple strings from a single pattern.
filename{,.bak}expands to:
filename filename.bakSo Bash actually executes:
cp filename filename.bakYou type the filename once, and Bash handles the rest.
Why This Is Better Than the “Normal” Way
Most people write:
cp filename filename.bakThat works — but brace expansion has advantages:
- Faster typing
- Fewer mistakes, especially with long filenames
- Cleaner commands
- Easier to scan in scripts and terminal history
Once you get used to it, going back feels slow.