Replacing file content

Linux Command Line Quick Reference

1 min read

Published Aug 18 2026


19
0
0
0

CLILinuxUbuntu

sed is a stream editor. It reads text, applies editing rules and writes the result. By default it does not alter the input file.

Replace text with sed

bash
sed 's/old/new/' file.txtsed 's/old/new/g' file.txtsed -i 's/old/new/g' file.txtsed -i.bak 's/old/new/g' file.txtsed -i 's|/old/path|/new/path|g' file.txt

s/old/new/ substitutes the first match on each line. The trailing g substitutes every match on each line. -i edits the file in place. -i.bak also keeps the original as a .bak backup.

The character after s is the delimiter. / is conventional, but another character such as | is useful when replacing paths containing /.

For literal replacements across many files, first use grep to confirm the affected files, then apply sed. Regular-expression metacharacters can change what the search expression means.

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
Linux Command Line Quick Reference | Replacing file content | SimpleSteps.guide