---
title: "How do I find and replace text in files from the command line?"
description: "GNU sed on Linux and BSD sed on macOS treat the -i flag differently. Learn the commands that work on each, a portable option, and how to replace text across many files."
url: https://www.danny.engineering/answer/bash/how-do-i-find-and-replace-text-in-files-from-the-command-line
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Bash"]
---

# How do I find and replace text in files from the command line?

## The short answer


Use `sed` with the `-i` flag to edit a file in place. The catch is that GNU sed (Linux) and BSD sed (macOS) disagree on how `-i` works. On macOS you must pass a backup suffix, even an empty one. Attaching a suffix directly to the flag works on both.


```bash
# Linux (GNU sed)
sed -i 's/old/new/g' file.txt

# macOS (BSD sed)
sed -i '' 's/old/new/g' file.txt

# Works on both, and keeps a backup as file.txt.bak
sed -i.bak 's/old/new/g' file.txt
```


## Why it happens


`sed` is a stream editor. By default it reads a file, applies your script and prints the result to standard output, leaving the original untouched. Editing in place is not part of the POSIX standard, so each implementation added `-i` in its own way:

- GNU sed treats the suffix as optional, and it must be attached to the flag, as in `-i.bak`. A bare `-i` means "no backup".
- BSD sed, which macOS ships, requires a suffix as the next argument. An empty string, `''`, means "no backup".

So the Linux command breaks on macOS, and the macOS command breaks on Linux. On macOS, `sed -i 's/old/new/g' file.txt` takes your script as the backup suffix and then tries to parse the file name as a script, which gives an error such as `invalid command code`. On Linux, `sed -i '' 's/old/new/g' file.txt` treats the empty string as the script and your real script as a file name, so it reports that it cannot read `s/old/new/g`.


## How to fix it


### Attach a backup suffix


The most portable form is a suffix glued to the flag. Both versions accept it. Delete the backups once you have checked the result:


```bash
sed -i.bak 's/http:/https:/g' config.yml
diff config.yml.bak config.yml
rm config.yml.bak
```


### Replace across many files


Let `find` pick the files and run `sed` on them. Ending `-exec` with `+` passes many files to each `sed` call, and file names with spaces are handled safely:


```bash
find src -type f -name '*.js' -exec sed -i.bak 's/oldName/newName/g' {} +
find src -type f -name '*.js.bak' -delete
```


To only touch files that contain the text, list them with `grep` first:


```bash
grep -rl --exclude-dir=.git 'oldName' src
```


### Use a different delimiter for paths


The character after `s` is the delimiter, so you do not have to escape every slash in a path:


```bash
sed -i.bak 's|/usr/local/bin|/opt/homebrew/bin|g' setup.sh
```


### Use the same sed everywhere


If you write scripts that run on both macOS and Linux, install GNU sed on your Mac with Homebrew. It is installed as `gsed` so it does not replace the system version:


```bash
brew install gnu-sed
gsed -i 's/old/new/g' file.txt
```


Alternatively, Perl's in-place mode behaves the same on both systems:


```bash
perl -pi -e 's/old/new/g' file.txt
```


## Common pitfalls

- Forgetting the `g` flag. Without it, `sed` replaces only the first match on each line.
- Not escaping special characters. In the pattern, `.`, `*`, `[` and the delimiter have special meaning. In the replacement, `&` inserts the whole match, so write `\&` for a literal ampersand.
- Running a replacement over `.git` or `node_modules`. Narrow the search with `find` options or `--exclude-dir`.
- Using single quotes when you need a variable. Use double quotes, as in `"s/old/$new/g"`, and remember that a slash inside the variable will break the command unless you change the delimiter.
- Expecting `\b` word boundaries to work everywhere. GNU sed supports them; on macOS use `[[:<:]]` and `[[:>:]]` instead, or use Perl.
- Skipping a dry run. Run the command without `-i` first, or pipe it through `diff`, to see what would change.

## Related reading

- [GNU sed manual](https://www.gnu.org/software/sed/manual/sed.html)
- [GNU findutils: running commands on files](https://www.gnu.org/software/findutils/manual/html_mono/find.html)
