<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Guy Rutenberg &#187; sed</title>
	<atom:link href="http://www.guyrutenberg.com/tag/sed/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Wed, 16 Jun 2010 19:53:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Batch Renaming Using sed</title>
		<link>http://www.guyrutenberg.com/2009/01/12/batch-renaming-using-sed/</link>
		<comments>http://www.guyrutenberg.com/2009/01/12/batch-renaming-using-sed/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 13:10:22 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=229</guid>
		<description><![CDATA[I was reorganizing my music library and decided to change the naming convention I&#8217;ve used. This task is just asking to be automated. Since the filename change could be described using regular expression, I looked for a way to use sed for the renaming process.
The files I had, had the following pattern as filename ARTIST [...]]]></description>
			<content:encoded><![CDATA[<p>I was reorganizing my music library and decided to change the naming convention I&#8217;ve used. This task is just asking to be automated. Since the filename change could be described using regular expression, I looked for a way to use <code>sed</code> for the renaming process.</p>
<p>The files I had, had the following pattern as filename ARTIST &#8211; SONG &#8211; TRACK &#8211; ALBUM</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">James Brown - I Got You (I Feel Good).ogg  - 01 - Classic James Brown</pre></div></div>

<p>I wanted to rename it to ARTIST &#8211; ALBUM &#8211; TRACK &#8211; NAME</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">James Brown - Classic James Brown - 01 - I Got You (I Feel Good).ogg</pre></div></div>

<p>Describing the change as a <code>sed</code> program is easy:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">s/\(.*\) - \(.*\) - \(.*\) - \(.*\).ogg/\1 - \4 - \3 - \2.ogg/</pre></div></div>

<p>Now all that has to be done is to pass each filename to mv and pass it again after it went through the <code>sed</code> script. This could be done like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*</span>; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #780078;">`echo $i | sed &quot;s/\(.*\) - \(.*\) - \(.*\) - \(.*\).ogg/\1 - \4 - \3 - \2.ogg/&quot;`</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>The important part is the</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;s/\(.*\) - \(.*\) - \(.*\) - \(.*\).ogg/\1 - \4 - \3 - \2.ogg/&quot;</span><span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>which pipes the filename to <code>sed</code> and returns it as an argument for <code>mv</code>.</p>
<p>To see what renaming will be done one can alter a bit the above command, and get</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*</span>; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>&quot;</span> <span style="color: #ff0000;">&quot;-&gt;&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #780078;">`echo $i | sed &quot;s/\(.*\) - \(.*\) - \(.*\) - \(.*\).ogg/\1 - \4 - \3 - \2.ogg/&quot;`</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>While will effectively  print a list of lines of the form <code>oldname -> newname</code>.</p>
<p>Of course this technique isn&#8217;t limited to the renaming I&#8217;ve done. By changing the pattern given to <code>sed</code>, one can do any kind of renaming that can be described as a regular expression replacement. Also one can change the globbing (the *) in the for loop to operate only on specific files, that match a given pattern, in the directory instead of all of them. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/01/12/batch-renaming-using-sed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.497 seconds -->
