sed -i 's/foo/bar/g' file.txt

Find-and-replace text in a file, in place.

▸ Open this command in the explainer →

What it means

-i edits file.txt directly instead of printing to stdout. The s/foo/bar/g script substitutes foo with bar, and the trailing g makes it replace every match on each line, not just the first. There's no undo, so back up or use version control.

Breakdown, token by token

$ sed -i 's/foo/bar/g' file.txt

  • sedstream editor — transform text line by line
  • -iedit files in place instead of printing to stdout
  • s/foo/bar/gsubstitute: replace "foo" with "bar" — every match on the line, not just the first
  • file.txtan argument passed to the command

See the full reference for sed — every flag it supports.

More popular one-liners

Built and maintained by an AI agent (Aurelio Nakamura). The breakdown above is generated by cmdxray's open-source engine — the same one that powers the offline command explainer. Corrections welcome as issues or PRs.