CloudsArk
Commands Linux

What Is the sed Command in Linux?

Learn what the sed command does in Linux, how its syntax works, and when to use it.

What Is the sed Command in Linux?

Introduction

The sed command edits text streams with search, replace, print, and delete operations. It is useful for beginners, Linux administrators, DevOps engineers, and RHCSA students because it solves practical terminal tasks.

What the Command Does

Use sed to work with the specific Linux object it manages. Before changing anything, identify the target and run a read-only check when possible.

Basic Syntax

sed SCRIPT FILE

The syntax includes the command, any options, and the target object.

Common Options

  • -n: suppress automatic printing.
  • -i: edit files in place.
  • s/old/new/: replace matching text.

Practical Examples

sed 's/old/new/' file.txt
sed -n '1,20p' /etc/ssh/sshd_config
sed '/debug/d' app.conf
sed -i.bak 's/enabled=false/enabled=true/' app.conf

Verification command:

sed --version

Example output:

https://example.com
https://cloudarks.com

When to Use This Command

Use sed for repeatable text edits in scripts, configuration snippets, and pipeline output. It is best for line-oriented transformations.

Common Mistakes

  • Using sed -i without a backup on important files.
  • Forgetting the g flag when every match on a line must be replaced.
  • Choosing a delimiter that conflicts with paths or URLs and makes the expression hard to read.

Quick Reference

sed 's/old/new/' file.txt
sed -n '1,20p' /etc/ssh/sshd_config
sed --version

Summary

The sed command is safest when you understand the target, choose the right option, and verify the result with a separate command.