In a large drive, I have multiple hundreds of files each named differently but with a few regular expressions. Each appended with random numbers and sometimes characters.
For example, there are 30 files named with a variation of the word "television", and another 50 varied from "lightbulb".
The thing is, that due to the bad coding practice I had years ago when implementing the script that generated the names, it's very inconsistent; so we might get:
- television139443.png
- elevision244904.png
- televisio097798.png
- elevisio984882.png
- _televisi90890890.png
- television-911181.png
You get the idea. That pattern applies to all the file "categories" -- "television", "lightbulb", "motorcar", etc. Luckily, there are at least 5 consistent characters in each filename which are not repeated in the other categories (a dozen cats).
What I'd like to achieve is to recursively go through the mixed folders, rename each file with the proper full title + appending creation date, and move them too their respective folder.
Sort of like:
case : regex("levis"):
rename to Television-($creation_date).($extension)
mv to ~/Categories/Television/
break;
case : regex("ghtbu"):
rename to Lightbulb-($creation_date).($extension)
mv to ~/Categories/Lightbulbs/
break;
Obviously this isn't proper code; it's just to illustrate the idea. I'm decently comfortable for everyday bash/zsh tasks but not fluent enough!
I would also need to gracefully handle cases where no regex is matched.
levis
fortelevision
,ghtbu
forlightbulb
, etc.find
then; and with an-exec
, you can rename those files. This is either really trivial, or I'm missing a point