Problem formatting a find with sed output

Hi I'm trying to clean a site from a js-trojan for a customer, it has added:

<script src=''></script> to all html-pages.

Since it's too many files to manually clean I tried to a do find like this:

find ./ -type f -exec sed -e "s\<script src=''></script>\ \g" {} > {} \;

Problem is you're not allowed to output to the input with sed. So I tried to do something like:

find ./ -type f ! -iname "*.new" -exec sed -e "s\<script src=''></script>\ \g" {} > {}.new \;

didn't work either, it outputs a file named "{}.new"...

Any tips on how to do this correct? Or another solution on how to clean this?


I think you are making things more complicated than they need to be. In particular, you want to use the -i flag, which allows you to edit the file in place like you want. You may want something like

sed -i '/<script src='http:\/\/\/3'><\/script>/ d' *.html

or use a script if you feel more comfortable, something like

for f in "dir/*.html"  
    sed -i '/<script src='http:\/\/\/3'><\/script>/ d' $f  



find . -type f -print0 | xargs -0 perl -i.nt02 -pe "s#<script src=''></script># #g"

It may not be entirely clear to you from @user379118's answer, but the -i flag (or --in-place) does allow you to do in-place editing with sed, optionally allowing you to create a copy of the original file as you go, just in case.

Thanks for your answers! I did like this:

while read f
    sed -i "s#<script src=''></script># #g" "$f"
done < list

list is a file containing paths to files, generated like this:

grep -irl "<script src=''></script>" ./folder/ > list

Need Your Help

Does GC collect Binding Mode OneTime?

c# wpf

I am wondering if Binding gets released/collected by GC when Binding Mode is OneTime?