Bash: remove numbers at the end of names.

前端 未结 3 585
温柔的废话
温柔的废话 2021-01-07 09:35

I have files like: alien-skull-2224154.jpg snow-birds-red-arrows-thunderbirds-blue-angels-43264.jpg dead-space-album-1053.jpg

How can I remove in bash the \"ID\" s

3条回答
  •  盖世英雄少女心
    2021-01-07 10:32

    This is the final code that works great. Thank you all for your time!

    for i in *.jpg; 
    do
    
    if [[  -e "${i%-*}.jpg" ]]; then
        num=1
        while [[ -e "${i%-*}-$num.jpg" ]]; do
            (( num++ ))
        done
     mv "$i" "${i%-*}-$num.jpg";
    
    else 
    rename 's/-\d+//' *.jpg
    fi
     done
    

提交回复
热议问题