Renaming multiple filenames using find and sed

Jun 16th, 2012 | By | Category: Linux / Freebsd

You thought you knew how to do this didn’t you?  For me my brain thought ‘oh that’s easy I just do..err wait.. Ok I have to think about this.. ok how about.. errr’ and then I went through this mental contortion act of how to get a variable declared using find and then I echo that and then sed replace and then and then and that’s when I gave up and googled it.. I found a bunch of samples that looked like they would work but it kept on failing to pipe the output of echo {} though sed and that’s when I found this on serverfault

find spec -name "*_test.rb" -exec sh -c 'echo mv "$1" "$(echo "$1" | sed s/test.rb\$/spec.rb/)"' _ {} \;
Yea, I'm still not sure why it works, or how it works or what that underscore is doing there but it works.. and you don't have to use sh if you dont' want it works with bash -c if your a puritan like me. Weird right? It's the quiet reminder that there is a whole other depth of understanding to these things that's just lying down there which I'll never know. Crazy.. 
Hope that helps, happy renaming!
Tags: , , , , ,

Leave a Comment