• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

Replace text in files under directory

I need to replace all instances of a sourced property file in all folders under a given directory.
so every instance of
. ~/abc/app_props.sh
replaced with
. ~/abc/app-props.sh

under ~/bin

Not sure how to do this.
0
jkavx
Asked:
jkavx
  • 2
1 Solution
 
woolmilkporcCommented:
find ~/bin -type f | xargs -I{} sed -i 's#. ~/abc/app_props.sh#. ~/abc/app-props.sh#' {}

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/app_props.sh#. ~/abc/app-props.sh#' $file > $file.$$
    mv $file.$$ $file
done


wmp
0
 
woolmilkporcCommented:
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/app_props.sh' $file ; then
       sed  's#. ~/abc/app_props.sh#. ~/abc/app-props.sh#' $file > $file.$$
       mv $file.$$ $file
    fi
done
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now