{"id":33,"date":"2012-10-10T14:53:09","date_gmt":"2012-10-10T12:53:09","guid":{"rendered":"http:\/\/blog.matthias.roehser.de\/?p=33"},"modified":"2014-07-05T15:51:27","modified_gmt":"2014-07-05T13:51:27","slug":"using-the-commandline-editor-sed-for-batch-renaming-of-files","status":"publish","type":"post","link":"https:\/\/blog.matthias.roehser.de\/?p=33","title":{"rendered":"Using the commandline editor &#8220;sed&#8221; for batch renaming of files"},"content":{"rendered":"<p>Given a set of files, that all end with &#8220;.png.jpg&#8221;. We want to remove the &#8220;.png&#8221; from the filename. This can be done using the linux shell pipes and the commandline editor &#8220;sed&#8221;.<\/p>\n<p>Examine this command: ls * | sed &#8216;s\/\\(.*\\)png\\.\\(.*jpg\\)\/mv &amp; \\1\\2\/&#8217; | sh<\/p>\n<p>The interesting part here is of course sed &#8216;s\/\\(.*\\)png\\.\\(.*jpg\\)\/mv &amp; \\1\\2\/&#8217;. The general syntax is &#8220;sed &#8216;s\/patternToSubstitue\/SubstituteBy\/'&#8221;.<\/p>\n<ul>\n<li>s means &#8220;substitute&#8221;<\/li>\n<li>\\(.*\\)png\\.\\(.*jpg\\) This splits the given string into a segment of arbitrary content \\(.*\\), another segment containing the fixed string &#8220;png.&#8221; and a third segment\u00a0\\(.*jpg\\) consisting of a string with arbitrary content ending with &#8220;jpg&#8221;. The first and third part are dynamically created, so sed allows to reference them using \\1 and \\2<\/li>\n<li>mv &amp; \\1\\2 This part constructs a command that can be executed by the shell. The &#8220;&amp;&#8221; references the original string passed into sed, the parts \\1 and \\2 are generated above and concatenated.<\/li>\n<\/ul>\n<p>The result of this command is, that the substring &#8220;png.&#8221; is removed from all filenames in the current directory. Files that don&#8217;t match this pattern are not changed, because the mv command gets the same name as source and target file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a set of files, that all end with &#8220;.png.jpg&#8221;. We want to remove the &#8220;.png&#8221; from the filename. This can be done using the linux shell pipes and the commandline editor &#8220;sed&#8221;. Examine this command: ls * | sed &#8216;s\/\\(.*\\)png\\.\\(.*jpg\\)\/mv &amp; \\1\\2\/&#8217; | sh The interesting part here is of course sed &#8216;s\/\\(.*\\)png\\.\\(.*jpg\\)\/mv &amp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-useful-tools"],"_links":{"self":[{"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33"}],"version-history":[{"count":6,"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions\/123"}],"wp:attachment":[{"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.matthias.roehser.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}