Replacing file content
Linux Command Line Quick Reference
1 min read
This section is 1 min read, full guide is 20 min read
Published Aug 18 2026
19
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
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.txts/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.