One problem with recursive renames is that whatever method you use to locate the files, it passes the whole path to rename, not just the file name. That makes it hard to do complex renames in nested folders.
I use find's -execdir action to solve this problem. If you use -execdir instead of -exec, the specified command is run from the subdirectory containing the matched file. So, instead of passing the whole path to rename, it only passes ./filename. That makes it much easier to write the regex.
find /the/path -type f \
-name '*.abc' \
-execdir rename 's/\.\/(.+)\.abc$/version1_$1.abc/' {} \;
In detail:
-type f means only look for files, not directories
-name '*.abc' means means only match filenames that end in .abc
- The backslashes after
-type and -name are the bash line-continuation character. I use them to make this example more readable, but they are not needed in practice.
- However, the backslash at the end of the
-execdir line is required. It is there to esacpe the semicolon, which terminates the command run by -execdir. Fun!
Explanation of the regex:
s/ start of the regex
\.\/ match the leading ./ that -execdir passes in. Use \ to escape the . and / metacharacters
(.+) match the filename. The parentheses capture the match for later use
\.abc escape the dot, match the abc
$ anchor the match at the end of the string
/ marks the end of the "match" part of the regex, and the start of the "replace" part
version1_ add this text to every file name
$1 references the existing filename, because we captured it with parentheses. If you use multiple sets of parentheses in the "match" part, you can refer to them here using $2, $3, etc.
.abc the new file name will end in .abc. No need to escape the dot metacharacter here in the "replace" section
/ end of the regex
Before
tree --charset=ascii
|-- a_file.abc
|-- Another.abc
|-- Not_this.def
`-- dir1
`-- nested_file.abc
After
tree --charset=ascii
|-- version1_a_file.abc
|-- version1_Another.abc
|-- Not_this.def
`-- dir1
`-- version1_nested_file.abc
Hint: rename's -n option is useful. It does a dry run and shows you what names it will change, but does not make any changes.