Replace text in files under directory

Posted on 2011-05-10
Last Modified: 2012-05-11
I need to replace all instances of a sourced property file in all folders under a given directory.
so every instance of
. ~/abc/
replaced with
. ~/abc/

under ~/bin

Not sure how to do this.
Question by:jkavx
    LVL 68

    Expert Comment

    find ~/bin -type f | xargs -I{} sed -i 's#. ~/abc/ ~/abc/' {}

    The above works if your sed implementation supports substitute-in-place ( -i ).

    If your sed doesn't support it do

    find ~/bin -type f | while read file; do
        sed  's#. ~/abc/ ~/abc/' $file > $file.$$
        mv $file.$$ $file

    LVL 68

    Accepted Solution

    We could make the whole thing a bit faster by avoiding to work on files which don't contain the search string:

    find ~/bin -type f | while read file; do
        if grep -q '. ~/abc/' $file ; then
           sed  's#. ~/abc/ ~/abc/' $file > $file.$$
           mv $file.$$ $file

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    About FreeBSD Jails In FreeBSD, jails are a way of doing operating system level virtualization.  The basis of FreeBSD jails is chroot (, which changes the root directory of processes.  As a…
    I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    This tutorial goes over how to archive and restore FreeBSD jails that are managed by ezjail.

    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