Jul 20 2016

Three ways of batch renaming files

Category: Uncategorizedguillem @ 2:49 PM

I’ve found myself having to change the name of a few files in batch by prepending something to them. Let’s see three easy ways to batch rename:

Universal way, works everywhere (we’re talking *nix OS here)


<span class="kwd">for</span><span class="pln"> filename </span><span class="kwd">in</span> <span class="pun">*.</span><span class="pln">jpg</span><span class="pun">;</span> <span class="kwd">do</span><span class="pln"> mv </span><span class="str">"$filename"</span> <span class="str">"prefix_$filename"</span><span class="pun">;</span> <span class="kwd">done</span><span class="pun">;

</span>
Little less universal but works if you have rename

<span class="pln">rename </span><span class="str">'s/(.*)$/new.$1/'</span><span class="pln"> original</span><span class="pun">.</span><span class="pln">filename

</span>
Lovely way in bash and zsh

<span class="pln">mv </span><span class="pun">{,</span><span class="pln">new</span><span class="pun">.}</span><span class="pln">original</span><span class="pun">.</span><span class="pln">filename</span>

 

Leave a Reply