With zsh
instead of bash
, to rename <digits>.svg
to A<digits>Solid.svelte
:
autoload -Uz zmv
To autoload
the zmv
batch-renaming function (best in ~/.zshrc
)
zmv -v '(<->).svg' 'A${1}Solid.svelte'
With perl-based renames, you can do the same (though without the safeguards of zmv
) with:
rename -v 's/^(\d+)\.svg\Z/A${1}Solid.svelte/' [0-9]*.svg
The [0-9]*.svg
expands to file names that start with a character in the 0 to 9 range (which includes but in some shells including bash
is not limited to 0123456789) and end in .svg
, but rename
will only rename those that are made only of ASCII decimal digits (0123456789) followed by .svg
.
Using a [0-9]*.svg
glob also means the file names won't start with -
, which means we don't need the --
which not all variants of perl
-based rename
support (while for others, omitting it introduces a command injection vulnerability!). Not all support -v
either.
If the intent is to capitalise the root names of the files and append Solid.svelte
, but also prepend A
to file names starting with a digits, for instance for foo-bar.svg
to become FooBarSolid.svelte
, 0.svg
A0Solid.svelte
and 0foo-bar.svg
A0FooBarSolid.svelte
as your code seems to be trying to do, you'd do:
zmv -v '([0-9]#)(*).svg' '${1:+A$1}${${(C)2}//-}Solid.svelte'
Or with rename
:
rename -v 's{^\./(\d*)(.*)\.svg\Z}{
($1 eq "" ? "" : "A$1") . ($2 =~ s/\w+/\u$&/gr =~ s/-//gr) . "Solid.svelte"
}ge' ./*.svg
Bearing in mind that contrary to zmv
, it only works properly on ASCII text (no captitalising of éric
to Éric
for instance).
Or you can extend the code you already have if you're happy with it by adding ; $_ = "A$_" if /^\d/
to prepend a A
if the file name starts with a decimal digit.
A
, but your code and examples imply that you'd like to also modify the rest of the name by addingSolid
before the extension. Is that correct? – Kusalananda May 16 '22 at 05:56mmv
- I'd start with a dry-run such asmmv -n -m -p '[0-9]*' 'A#1#2'
, then remove the-n
when I'm happy with the output. – Toby Speight May 16 '22 at 17:18