Hi could anyone help me? I'm trying to strip leading digits from multiple mp3 files so
01 some_file.mp3
would become some_file.mp3
.
If any one could show me how to do it with zmv
that would be great, thanks.
Hi could anyone help me? I'm trying to strip leading digits from multiple mp3 files so
01 some_file.mp3
would become some_file.mp3
.
If any one could show me how to do it with zmv
that would be great, thanks.
This solution relies on Bash parameter expansion with substitution.
# Generate some dummy files for this demonstration
for i in {0..2}{0..9} ; do touch "$i some_file$i.mp3" ; done
# Rename, stripping two leading digits and a space
for i in [0-9][0-9]" "*.mp3 ; do mv "$i" "${i/[0-9][0-9] /}"; done
Use extended pattern matching:
shopt -s extglob
for F +([[:digit:]])*([[:blank:]])*.mp3; do
mv -v -- "$F" "${F##+([[:digit:]])*([[:blank:]])}"
done
Or recursive:
shopt -s extglob
function remove_leading_digits {
local A B
for A; do
B=${1##+([[:digit:]])*([[:blank:]])}
[[ $A != "$B" ]] && mv -v -- "$A" "$B"
done
}
readarray -t FILES < <(exec find your_dir -type f -regextype posix-egrep -regex '[[:digit:]]+[[:blank:]]*.mp3')
remove_leading_digits "${FILES[@]}"
You can save the function to work generally for a script:
#!/bin/ash
shopt -s extglob
function remove_leading_digits {
local A B
for A; do
B=${1##+([[:digit:]])*([[:blank:]])}
[[ $A != "$B" ]] && mv -v -- "$A" "$B"
done
}
remove_leading_digits "$@"
And run it with
bash script.sh files
Like
shopt -s extglob
bash script.sh +([[:digit:]])*.mp3
Or just
bash script *.mp3 ## Still safe but slower.