• Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Find and Replace Text using sed Command

Written by Rahul, Updated on October 11, 2018

Sed is known as Stream Editor, a tool is designed to find and replace text in the text file. It’s very useful like you need to change any word in multiple files and you don’t want to do it manually because of its time killing.

Replace Text in Same Single

The following command will search for all string “Hello” in /opt/docs/welcome.txt file. The replace it with “Howdy” in the same file.

sed -i 's/Hello/Howdy/g' /opt/docs/welcome.txt

Replace Text in All files

The Following command is helpful to replace string in multiple files at once in a directory /opt/docs. Here find will navigate to all files under the defined directory and execute sed command one by one.

find /opt/docs/ -type f -exec sed -i 's/Hello/Howdy/g' {} ;

Replace Text with Keep Original

If you do not want to make changes in your original file and create new files with changed strings, use following command.

sed 's/Hello/Howdy/g' /opt/docs/welcome.txt > /opt/docs/welcome_2.txt

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

1 Comment

  1. Avatar sunny Reply
    October 26, 2017 at 10:21 am

    Hi,

    Above command Not Working
    find /opt/docs/ -type f -exec sed -i ‘s/Hello/Howdy/g’ {} ;
    getting the below error
    find: missing argument to `-exec’

    solution:
    A -exec command must be terminated with a ; (so you usually need to type \;

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Debian 10
  • Download Ubuntu 20.04 LTS – DVD ISO Images
  • Linux Run Commands As Another User
  • How to Check PHP Version (Apache/Nginx/CLI)
  • How To Install and Configure GitLab on Ubuntu 20.04
  • How to Install PyCharm on Ubuntu 20.04
  • How to Check Ubuntu Version with Command or Script
  • How to Set all directories to 755 And all files to 644
© 2013-2021 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy