Search code examples
htmlcssfedora20

How to modify a lot of html files at the same time?


I have thousands of HTML files that I would like to modify at the same time on a Fedora system. It would be to replace style.css by ../headers.css and to replace another long chain that you can see here : http://pastebin.com/PHAz8Q4C


Solution

  • I'd recommend using a tool like sed which can do text/regex replacements on a file.

    Combining this with find and xargs is a good start to making it work on a large number of files.

    So, for example, you could do something like

    find -name "*.html" -print0 | xargs -0 sed -i 's#style.css#../headers.css#g'

    Since there is no way to undo this operation, I would recommend making a backup of the files just in case (or use version control!)

    EDIT: Guide on how to expand this to other search/replace terms (like your pastebin):

    Same idea. Just make sure you escape everything properly. As an explanation that can get you further...

    • find finds all the html files
    • xargs runs the command after it on each file
    • sed does the replacement
    • -i tells sed to do it in place
    • s tells it to Substitute
    • style.css is what to replace
    • ../headers.css is what to replace it with
    • and g tells it to replace all occurrences in the file.