{"id":5079,"date":"2016-09-22T01:52:10","date_gmt":"2016-09-22T01:52:10","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5079"},"modified":"2016-09-22T01:52:10","modified_gmt":"2016-09-22T01:52:10","slug":"replace-files-recursively-sed","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/","title":{"rendered":"Replace in files recursively with sed"},"content":{"rendered":"<p>Replacing text in many files is reasonably simple with sed, on the command line.<\/p>\n<p>If you do this, I would recommend that you come up with a way to take a backup, because there is almost no chance you&#8217;ll get it right on the first try.<\/p>\n<p>My preferred approach is to make an empty git repository with the initial state, which allows me to rollback the changes every time I need to iterate on the sed script.<\/p>\n<pre lang=\"bash\">\ngit init .\ngit add -A \ngit commit -m \"files\"\n<\/pre>\n<p>For this example, I&#8217;m adding an Amazon link to a bunch of files. The successful solution is as follows (you want the generate format of &#8220;s\/<search>\/<replacement>\/&#8221; to control it in your own script).<\/p>\n<pre lang=\"bash\">\nfind . -type f -iname '*.html' -exec sed -i 's\/\"sidebar-module\">\/\"sidebar-module\"><iframe style=\"width:120px;height:240px;\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" frameborder=\"0\" src=\"\\\/\\\/ws-na.amazon-adsystem.com\\\/widgets\\\/q?ServiceVersion=20070822\\&#038;OneJS=1\\&#038;Operation=GetAdHtml\\&#038;MarketPlace=US\\&#038;source=ss\\&#038;ref=as_ss_li_til\\&#038;ad_type=product_link\\&#038;tracking_id=thesecrelifeo-20\\&#038;marketplace=amazon\\&#038;region=US\\&#038;placement=0981531687\\&#038;asins=0981531687\\&#038;linkId=0dc4250b21bf0e833004fd3e58765806\\&#038;show_border=true\\&#038;link_opens_in_new_window=true\"><\\\/iframe>\/' \"{}\" +;\n<\/pre>\n<p>Once you create this script, you should check it in.<\/p>\n<pre lang=\"bash\">\n...add script...\ngit add replace.sh\ngit commit -m \"script\"\n<\/pre>\n<p>I found there were several problems getting this to work. You need to escape &#038; and \\, and make sure to replace what you removed &#8211; it&#8217;s often helpful to use a chunk of text that comes before or after the replacement.<\/p>\n<p>When you make a mistake, you can just do<\/p>\n<pre lang=\"bash\">\ngit reset --hard\n<\/pre>\n<p>As long as you&#8217;re committing the replacement script as you go.<\/p>\n<p>If you like, you can get rid of the git repository once you&#8217;re done, by doing this:<\/p>\n<pre lang=\"bash\">\nrm -rf .git\n<\/pre>\n<p>~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to replace text in a large number of files on the command line<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[12],"tags":[249,459,500],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"How to replace text in a large number of files on the command line\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"gary\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Gary Sieling - Software Engineer\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Replace in files recursively with sed - Gary Sieling\" \/>\n\t\t<meta property=\"og:description\" content=\"How to replace text in a large number of files on the command line\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2016-09-22T01:52:10+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2016-09-22T01:52:10+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Replace in files recursively with sed - Gary Sieling\" \/>\n\t\t<meta name=\"twitter:description\" content=\"How to replace text in a large number of files on the command line\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#blogposting\",\"name\":\"Replace in files recursively with sed - Gary Sieling\",\"headline\":\"Replace in files recursively with sed\",\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"},\"datePublished\":\"2016-09-22T01:52:10+00:00\",\"dateModified\":\"2016-09-22T01:52:10+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#webpage\"},\"articleSection\":\"How-To, git, regexes, sed\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/how-to\\\/#listItem\",\"name\":\"How-To\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/how-to\\\/#listItem\",\"position\":2,\"name\":\"How-To\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/how-to\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#listItem\",\"name\":\"Replace in files recursively with sed\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#listItem\",\"position\":3,\"name\":\"Replace in files recursively with sed\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/how-to\\\/#listItem\",\"name\":\"How-To\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/\",\"name\":\"gary\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"gary\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#webpage\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/\",\"name\":\"Replace in files recursively with sed - Gary Sieling\",\"description\":\"How to replace text in a large number of files on the command line\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/replace-files-recursively-sed\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"datePublished\":\"2016-09-22T01:52:10+00:00\",\"dateModified\":\"2016-09-22T01:52:10+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Replace in files recursively with sed - Gary Sieling","description":"How to replace text in a large number of files on the command line","canonical_url":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#blogposting","name":"Replace in files recursively with sed - Gary Sieling","headline":"Replace in files recursively with sed","author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"},"datePublished":"2016-09-22T01:52:10+00:00","dateModified":"2016-09-22T01:52:10+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#webpage"},"isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#webpage"},"articleSection":"How-To, git, regexes, sed"},{"@type":"BreadcrumbList","@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.garysieling.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/#listItem","name":"How-To"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/#listItem","position":2,"name":"How-To","item":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#listItem","name":"Replace in files recursively with sed"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#listItem","position":3,"name":"Replace in files recursively with sed","previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/#listItem","name":"How-To"}}]},{"@type":"Organization","@id":"https:\/\/www.garysieling.com\/blog\/#organization","name":"Gary Sieling","description":"Software Engineer","url":"https:\/\/www.garysieling.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author","url":"https:\/\/www.garysieling.com\/blog\/author\/gary\/","name":"gary","image":{"@type":"ImageObject","@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g","width":96,"height":96,"caption":"gary"}},{"@type":"WebPage","@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#webpage","url":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/","name":"Replace in files recursively with sed - Gary Sieling","description":"How to replace text in a large number of files on the command line","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/#breadcrumblist"},"author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"creator":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"datePublished":"2016-09-22T01:52:10+00:00","dateModified":"2016-09-22T01:52:10+00:00"},{"@type":"WebSite","@id":"https:\/\/www.garysieling.com\/blog\/#website","url":"https:\/\/www.garysieling.com\/blog\/","name":"Gary Sieling","description":"Software Engineer","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Gary Sieling - Software Engineer","og:type":"article","og:title":"Replace in files recursively with sed - Gary Sieling","og:description":"How to replace text in a large number of files on the command line","og:url":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/","article:published_time":"2016-09-22T01:52:10+00:00","article:modified_time":"2016-09-22T01:52:10+00:00","twitter:card":"summary_large_image","twitter:title":"Replace in files recursively with sed - Gary Sieling","twitter:description":"How to replace text in a large number of files on the command line"},"aioseo_meta_data":{"post_id":"5079","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2023-02-04 17:05:33","updated":"2026-07-06 02:13:39","ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\/category\/how-to\/\" title=\"How-To\">How-To<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tReplace in files recursively with sed\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.garysieling.com\/blog"},{"label":"How-To","link":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/"},{"label":"Replace in files recursively with sed","link":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/"}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5079"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5079"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5079\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}