批量重命名多个mp3的剥离前导数字

时间:2014-06-12 03:06:14

标签: zsh batch-rename

你可以帮助我吗?我试图从多个mp3文件中删除前导数字 01 some_file.mp3将成为some_file.mp3

如果有人可以告诉我如何使用zmv来做这件事,那就太棒了,谢谢。

2 个答案:

答案 0 :(得分:3)

此解决方案依赖于Bash parameter expansion替换。

# 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

答案 1 :(得分:1)

使用扩展模式匹配:

shopt -s extglob
for F +([[:digit:]])*([[:blank:]])*.mp3; do
    mv -v -- "$F" "${F##+([[:digit:]])*([[:blank:]])}"
done

或递归:

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[@]}"

您可以将该功能保存为通常用于脚本:

#!/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 "$@"

运行它
bash script.sh files

shopt -s extglob
bash script.sh +([[:digit:]])*.mp3

或者只是

bash script *.mp3  ## Still safe but slower.