Tuesday, October 18, 2022

Batch Renaming Files with Wildcards

First make sure you've got your files version controlled, because this has a way of going horribly wrong and you want to be able to reset to your starting state when it does.

On debian 11, you want the rename package

sudo apt install rename

This provides the perl renamer. Check you've got the right one, because there are many different rename commands provided by different packages!

$ rename -V
/usr/bin/rename using File::Rename version 1.13, File::Rename::Options version 1.10

find a way to isolate all the files you want to rename using shell wildcards

$ ls account*

'accounts 2008-2009.ods'  'accounts 2011-2012.ods'  'accounts 2014-2015.ods'

Now come up with a perl/sed replacement expression that changes the spaces to dashes

$ rename 's/\ /-/' account*

$ ls account*
accounts-2008-2009.ods  accounts-2011-2012.ods  accounts-2014-2015.ods