Easiest way to search thru files and replace various strings

Posted on 2005-04-15
Last Modified: 2013-12-27

I need to replace various strings in certain files that can be located in various subdirectories and levels underneath a main directory. I am way rusty with my shell and sed, so was looking here for some help. I would attempt this with sed but if there is an easier that would be great.

My script currently processes each directory and performs some other tasks. I now need to add a line or two that will search and replace these strings I am referring to.

Question by:rhugga
    LVL 14

    Accepted Solution

    Yes, sed is the way to go here.

    Something like this:

    umask 077
    sed 's|original text|replacement text|g' < "$file" > "$file.tmp" \
    && cp "$file.tmp" "$file"
    rm -f "$file.tmp"
    LVL 1

    Author Comment


    Didn't realize it would be that simple, heh.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
    Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now