Editing text files from the command line is one of those skills that quietly saves hours over time. Among Unix and Linux tools, sed is a classic stream editor, and its -i option is especially useful because it lets you modify files in place instead of printing changes to the terminal.
TLDR: The sed -i command edits a file directly, commonly used for quick search-and-replace tasks such as sed -i 's/old/new/g' file.txt. In a realistic sysadmin scenario, updating a deprecated URL across 250 configuration files can take seconds instead of 30 minutes of manual editing. Use it carefully, because changes are written directly to disk, and syntax differs slightly between GNU/Linux and macOS. When in doubt, create a backup with sed -i.bak.
What Does sed -i Mean?
The command sed stands for stream editor. By default, it reads input line by line, applies editing instructions, and prints the result to standard output. That means if you run a basic sed substitution, your file is not changed unless you redirect the output somewhere.
The -i option changes that behavior. It tells sed to edit the file in place, meaning the original file contents are overwritten with the modified version.
For example:
sed -i 's/apple/orange/g' fruits.txt
This replaces every occurrence of apple with orange inside fruits.txt.
Basic Syntax of sed -i
The general syntax looks like this:
sed -i[backup_extension] 'command' filename
Breaking it down:
sed: Runs the stream editor.-i: Enables in-place editing.[backup_extension]: Optional suffix used to create a backup file.'command': The editing instruction, often a substitution.filename: The file you want to modify.
The most common sed command is substitution:
s/search/replacement/flags
Here, s means substitute. The search part is the text or pattern you want to find, replacement is what you want to insert, and flags control how the replacement behaves.
Common Example: Search and Replace
Suppose you have a file named config.txt containing this line:
server_name=oldsite.com
You can replace the domain with:
sed -i 's/oldsite.com/newsite.com/' config.txt
After running the command, the file becomes:
server_name=newsite.com
Notice that this replaces only the first match on each line. If a line contains multiple occurrences and you want to replace all of them, add the g flag:
sed -i 's/oldsite.com/newsite.com/g' config.txt
The g stands for global, meaning every match on the line is replaced.
Creating a Backup Before Editing
Because sed -i changes the file directly, it is wise to create a backup when working with important files. You can do this by adding an extension after -i:
sed -i.bak 's/debug=true/debug=false/g' app.conf
This modifies app.conf and creates a backup named app.conf.bak. If something goes wrong, you can restore the original:
mv app.conf.bak app.conf
This simple habit can prevent frustrating mistakes, especially when performing broad replacements across multiple files.
GNU/Linux vs macOS Syntax
One of the most common surprises with sed -i is that it behaves differently on GNU/Linux and macOS. Linux systems usually use GNU sed, while macOS uses BSD sed.
On GNU/Linux, this works:
sed -i 's/foo/bar/g' file.txt
On macOS, you often need to provide an empty backup extension:
sed -i '' 's/foo/bar/g' file.txt
If you want a backup on macOS, use:
sed -i '.bak' 's/foo/bar/g' file.txt
This difference is especially important when writing shell scripts meant to run on multiple operating systems. A command that works perfectly on a Linux server may fail on a developer’s MacBook unless portability is considered.
Editing Multiple Files
sed -i becomes powerful when combined with file patterns. For example, to replace a company name across all text files in a directory:
sed -i 's/Old Company/New Company/g' *.txt
To edit files recursively, combine find with sed:
find . -name "*.html" -exec sed -i 's/http:/https:/g' {} \;
This command searches the current directory and subdirectories for HTML files, then replaces http: with https:. For a website migration with hundreds of pages, this is far faster and less error-prone than opening each file manually.
Deleting Lines with sed -i
sed -i is not limited to replacements. You can also delete lines. For example, to remove all lines containing the word deprecated:
sed -i '/deprecated/d' notes.txt
The pattern /deprecated/ matches lines containing that word, and d deletes them.
To delete a specific line number, such as line 5:
sed -i '5d' notes.txt
To delete a range of lines, such as lines 10 through 20:
sed -i '10,20d' notes.txt
Replacing Text Only on Matching Lines
Sometimes you want to replace text only when a line matches a certain condition. For example, imagine a file with multiple environments:
dev_url=http://dev.example.com
prod_url=http://example.com
To replace http with https only on the production line:
sed -i '/prod_url/s/http/https/' settings.conf
This says: find lines containing prod_url, then perform the substitution only on those lines.
Using Different Delimiters
By convention, sed substitution uses forward slashes:
sed -i 's/path/to/file/new/path/g' file.txt
But this becomes messy when working with URLs or file paths because slashes must be escaped. Instead, you can use a different delimiter, such as |:
sed -i 's|/var/www/old|/var/www/new|g' server.conf
This is easier to read and reduces the chance of syntax errors.
Common Use Cases for sed -i
Here are practical situations where sed -i is especially useful:
- Updating configuration files: Change ports, domains, feature flags, or environment values.
- Codebase refactoring: Rename variables, functions, or package paths across many files.
- Website migrations: Replace old URLs, convert
httplinks tohttps, or update asset paths. - Log cleanup: Remove noisy lines or redact sensitive values from copied logs.
- Automation scripts: Modify templates during deployment or build processes.
Important Safety Tips
Because sed -i can change many files quickly, a small mistake can spread just as quickly. Before running it on valuable files, follow these habits:
- Test without
-ifirst: Run the command without in-place editing to preview the output. - Use backups: Add an extension like
.bakwhen editing important files. - Quote your command: Use single quotes to prevent the shell from interpreting special characters.
- Check your pattern: Make sure your search expression is not too broad.
- Use version control: If editing code, commit or stash changes before running bulk replacements.
Final Thoughts
The sed -i command is a compact but powerful tool for editing files directly from the terminal. Its most common use is search and replace, but it can also delete lines, target specific patterns, and automate repetitive editing tasks across entire projects. Once you understand its syntax and platform differences, sed -i becomes one of the most useful commands in your command-line toolkit. Just remember: with in-place editing, speed is a benefit, but caution is a requirement.
