View script Copied!
Atomically swap two files by renaming. Useful for rotating config files (config.prod ↔ config.dev), A/B testing file variants, or any quick swap without a temporary directory.
Works in place using a temp file as intermediary. On partial failure, attempts to restore the original state.
$ ls
config.prod config.dev
$ swap config.prod config.dev
$ ls
config.prod config.dev
# Now config.prod has dev content, config.dev has prod content
Rotate active config with backup:
swap nginx.conf nginx.conf.old
Swap test data files for different test runs:
swap dataset-A.csv dataset-B.csv
Restore a previous version (when you've been swapping back and forth):
# First time: current ↔ backup
swap app.js app.js.backup
# Later: swap back
swap app.js app.js.backup
| Flag | Description |
|---|---|
-h, --help |
Display help |
| Code | Meaning |
|---|---|
| 0 | Success |
| 2 | Not enough arguments (usage error) |
| 4 | First file does not exist |
| 5 | First file is a directory |
| 6 | Second file does not exist |
| 7 | Second file is a directory |
| 8 | Failed to move first file to temp |
| 9 | Failed to move second file to first location |
| 10 | Failed to move temp to second location |
None (uses only bash built-ins and mv).
/tmp/temp-swap-<basename> as the temporary location (adds PID and random suffix if that path already exists)