Find the answer to your Linux question:
Results 1 to 2 of 2
hi, I need to change multiple files located in different directories and i can by using SED: for f in `find . -type f`; do sed -i 's/old/new/g' $f; done ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    9

    Changing multiple files with SED

    hi,
    I need to change multiple files located in different directories
    and i can by using SED:

    for f in `find . -type f`; do sed -i 's/old/new/g' $f; done

    However, ALL dates of the files will be changed
    even if nothing has been changed inside the file

    so i want only the date file renewed when the file has been modified!

    how can i adhieve this?
    Cheerz
    Jolan

  2. #2
    Linux Newbie jpalfree's Avatar
    Join Date
    Jul 2005
    Location
    Montreal, CA
    Posts
    198
    well, i'm not sure how to do it in general...

    but in your specific case, sed will only modify the file when it finds your "old" expression in the file... so why not use grep first to see if "old" is actually in the file. You could try something like:

    Code:
    for f in `find . -type f`; do 
    grep -q 'old' $f &&
    sed -i 's/old/new/g' $f #will only execute if sed finds what it's looking for
    done
    Avatar from xkcd.com, a hilarious computer related webcomic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...