Rename all files with the name pattern *.[a-z0-9].bundle.*, to replace the [a-z0-9] with a given string
In pure bash
regEx
using the =~
variable (supported from bash 3.0
onwards)
#!/bin/bashstring_to_replace_with="sample"for file in *.jsdo [[ $file =~ \.([[:alnum:]]+).*$ ]] && string="${BASH_REMATCH[1]}" mv -v "$file" "${file/$string/$string_to_replace_with}"done
For your given input files, running the script
$ bash script.shinline.d41d8cd.bundle.js -> inline.sample.bundle.jsmain.6d2e2e89.bundle.js -> main.sample.bundle.js
Short, powerfull and efficient:
Use this (perl) tool. And use Perl Regular Expression:
rename 's/\.\X{4,8}\./.myString./' *.js
or
rename 's/\.\X+\./.myString./' *.js
A pure-bash option:
shopt -s extglob # so *(...) will workgeneric_string="foo" # or whatever else you want between the dotsfor f in *.bundle.js ; do mv -vi "$f" "${f/.*([^.])./.${generic_string}.}"done
The key is the replacement ${f/.*([^.]./.${generic_string}.}
. The pattern /.*([^.])./
matches the first occurrence of .<some text>.
, where <some text>
does not include a dot ([^.]
) (see the man page). The replacement .${generic_string}.
replaces that with whatever generic string you want. Other than that, double-quote in case you have spaces, and there you are!
Edit Thanks to F. Hauri - added -vi
to mv
. -v
= show what is being renamed; -i
= prompt before overwrite (man page).