A robust Bash utility to safely replace-allrings across an entire codebase, including:
- Namespace updates (PHP, Symfony, etc.)
- File path transformations
- Escaped string replacements (e.g.
composer.json, JSON configs) - File and directory renaming
Designed for large projects where consistency and precision are critical.
- ✅ Multi-format replacement:
- Namespace (
App\Services) - Path (
App/Services) - Escaped namespace (
App\\Services)
- Namespace (
- ✅ Recursive file content updates
- ✅ File & directory renaming
- ✅ Safe dry-run mode
- ✅ Binary-safe processing
- ✅ Ignore common build/system directories
- ✅ No
sedescaping issues (uses Python for exact byte replacement)
- Bash (Linux, macOS, WSL)
python3- Standard Unix tools:
grep,find,awk,mv
./replace-all <OLD_STRING> <NEW_STRING> [dry-run: true|false]./replace-all "App\\Services" "App\\Modules\\Services"
⚠️ Important: Always use double backslashes (\\) in terminal input for namespaces.
Preview changes without modifying files:
./replace-all "App\\Services" "App\\Modules\\Services" trueOutput will show:
- Files that would be modified
- Rename operations that would occur
The script automatically generates 3 replacement patterns:
| Type | Example Input | Generated |
|---|---|---|
| Namespace | App\Services |
App\Services |
| Path | App\Services |
App/Services |
| Escaped Namespace | App\Services |
App\\Services |
- Uses
grepto locate files containing target strings - Uses Python byte-level replacement to avoid encoding/escaping issues
- Processes only matching files for efficiency
- Detects paths containing the target string
- Renames them safely
- Applies deep-first renaming to avoid conflicts
The script automatically skips:
.gitnode_modulesvendordist.idea.vscodestorage
You can modify this list in:
IGNORE_DIRS=( ...)-
Always run dry-run first on large codebases
-
Commit your code before running:
git commit -am "Before replace-all ``` -
This script performs global replacements — no semantic parsing
After running the script (especially for PHP projects):
composer dump-autoloadThis ensures class autoloading is properly refreshed.
- Refactoring Symfony namespaces
- Migrating modules (
App\Core→App\Modules\Core) - Renaming domain layers
- Updating legacy folder structures
- Cleaning up inconsistent path formats
- Ensure the string exists in the project
- Try dry-run to confirm detection
- Verify escaping (
\\vs\) - Check generated variants
chmod +x replace-allThis script is built for precision replace-all at scale:
- No regex pitfalls
- No partial replacements
- No broken paths
Use it when simple tools like sed or IDE replace become unreliable.
---
If you want, I can extend this with:
- CLI flags instead of positional args
- Parallel processing (for large repos)
- Git-aware mode (only changed files)
- Rollback support
Just tell me.
MIT License
Copyright (c) 2026 mduvernon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.