So you have a group files with an unuseful name for another program or service you are using and you want to rename the whole group.

Here is the solution:

renamer.sh
#!/bin/bash
# renames.sh
# basic file renamer

criteria=$1
re_match=$2
replace=$3

for i in $( ls *$criteria* ); 
do
   src=$i
   tgt=$(echo $i | sed -e "s/$re_match/$replace/")
   mv $src $tgt
done

It is used as such:

./renamer.sh csv '201606' '2016-06'

It uses sed and that allows all of the regex you might be familiar with, a la:

'[0-9]{2}'

I won’t go into heavy regex usage here, there are several good articles/guides out there on that. One day I might post a conglomeration of those with most used by myself at the top with any additional items I’ve found that are helpful.

Also, I definitely found this and copied it verbatim from: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-12.html which is a great resource for anyone wanting to expand their knowledge/skills on a well organized knowledge system. Thank you TLDP, you’ve been a great resource for years to date. ^_^

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.