重击:删除名称末尾的数字. [英] Bash: remove numbers at the end of names.

查看:70
本文介绍了重击:删除名称末尾的数字.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的文件: Alien-skull-2224154.jpg 雪鸟红色箭头箭头雷鸟蓝色天使43264.jpg dead-space-album-1053.jpg

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

如何在bash中删除.jpg之前的"ID"字符串 id始终由前单词-"分隔 谢谢.

How can I remove in bash the "ID" string before .jpg The id is always separated by the before word with "-" Thanks.

推荐答案

这是使用 bash参数替换:

for i in *.jpg; do mv "$i" "${i%-*}.jpg"; done

或者在更一般的情况下(例如,如果您具有其他文件扩展名),请尝试:

Or for the more general case (i.e. if you have other file extensions), try:

for i in *.*; do mv "$i" "${i%-*}.${i##*.}"; done

结果:

alien-skull.jpg
dead-space-album.jpg
snow-birds-red-arrows-thunderbirds-blue-angels.jpg


根据以下评论,尝试以下bash脚本:


As per the comments below, try this bash script:

declare -A array

for i in *.*; do

    j="${i%-*}.${i##*.}"

    # k="$j"
    # k="${i%-*}-0.${i##*.}"

    for x in "${!array[@]}"; do

        if [[ "$j" == "$x" ]]; then
            k="${i%-*}-${array[$j]}.${i##*.}"
        fi
    done

    (( array["$j"]++ ))

    mv "$i" "$k"
done

请注意,您将需要取消注释k的值,具体取决于您希望格式化文件名的方式.如果取消第一行的注释,则只有重复的基本名称将递增:

Note that you will need to uncomment a value for k depending on how you would like to format the filenames. If you uncomment the first line, only the duplicate basenames will be incremented:

dead-space-album.jpg
dead-space-album-1.jpg
dead-space-album-2.jpg
dead-space-album-3.jpg

如果取消第二行的注释,则会得到以下内容:

If you uncomment the second line, you'll get the following:

alien-skull-0.jpg
alien-skull-1.jpg
alien-skull-2.jpg
alien-skull-3.jpg

这篇关于重击:删除名称末尾的数字.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆