I have a bunch of files containing prefix codes in brackets. I would like to turn these prefixes into suffixes, for example:
Finance-(4BF)-001.doc --> Finance-001-4BF.doc Corporate-(K04)-001.doc --> Corporate-001-K04.doc
I previously wrote a very simple VBScript for this on a Windows machine, but now I need to do this on Linux. After some tedious searching, I can't find a simple and elegant way to apply a regular expression to a file name and rename it using regular expression matches.
So far I have this:
#!/bin/bash for i in *.doc do x=${i//[\(\)]/} echo "$i renames to: $x" done
The result of this:
Corporate-(K04)-001.doc renames to: Corporate-K04-001.doc Finance-(4BF)-001.doc renames to: Finance-4BF-001.doc
I know that the regular expression above just strips the brackets () from the file name ... but there certainly should be a way to match their contents (e.g. \((\w)\) ) and then use this match in rename command (e.g. $1 )?
Also, being a Linux newbie, I donโt understand what x=${...} does, and since I donโt know what he called, I cannot use it. I assume that it applies the regular expression to line i , but in this case why can't I extract matches from it like $1 and $2 , etc.
Thanks!
source share