How to Find and Replace Text in Multiple .txt Files
Renaming a project, fixing a repeated typo or updating a contact in dozens of text files by hand is slow and error-prone. These are the common ways to search and replace across many .txt files, with the trade-offs of each.
Before you start: back up
Replacing text in files cannot be undone in most tools. Copy the folder first, run the replacement, check a few files, and only then delete the backup.
Option 1: A multi-note editor (up to 100 files)
With AllInNote you open your files as notes, type the word in Find in notes and see which notes contain it, each outlined in red. Then enter the old and new text and press Replace for the selected note, or Replace all for every matching note. You see the effect immediately and the files are saved automatically. Pinned notes are skipped. Details are in Find and replace across many notes.
Option 2: PowerShell on Windows
For many files or whole folder trees, a short script is faster. This example replaces text in every .txt file in the current folder and its subfolders:
Get-ChildItem -Filter *.txt -Recurse | ForEach-Object {
$t = Get-Content $_.FullName -Raw -Encoding UTF8
$t -replace 'old text', 'new text' | Set-Content $_.FullName -Encoding UTF8 -NoNewline
}
Notes: the -replace operator treats the search text as a regular expression, so characters such as a dot or parentheses have special meaning; escape them or use the .Replace() string method for literal text. In Windows PowerShell 5.1, -Encoding UTF8 writes a byte order mark, which most programs accept but some do not.
Option 3: sed on Linux or macOS
find . -name '*.txt' -exec sed -i 's/old text/new text/g' {} +
On macOS, the built-in sed needs an empty backup suffix: sed -i '' 's/old/new/g'. Escape slashes in your text or choose another delimiter such as |.
Option 4: A dedicated search-and-replace tool
Free and paid desktop tools can replace text across folders with previews, filters and backups. They are a good choice for very large numbers of files or for patterns that need regular expressions. Read their documentation, and test on a copy first.
Which should you choose?
- Under about 100 short notes and you want to see them: a multi-note editor.
- Hundreds or thousands of files: a script or a dedicated tool.
- Complex patterns: regular expressions in a script or a tool that supports them.
Checklist for any method
- Back up the folder.
- Search for the old text first and confirm which files match.
- Watch out for short words that also appear inside longer words.
- Run the replacement, then search for the new text and open a few files to verify.