开发者

Convert files names into integers

So I have:

01.jpg 02.jpg 开发者_如何学Python3.jpg 4.jpg 05.jpg

and want to make them all like below using a shell script or a command on linux

1.jpg 2.jpg 3.jpg 4.jpg 5.jpg


If you have the command rename on your system,

rename "s/0(\\d+\\.jpg)/\$1/" *.jpg


for i in 0*.jpg; do
    mv $i ${i:1}
done


To remove any number of zeros from the start and prevent collisions:

for old in 0*.jpg; do
    new=$(echo ${old} | sed 's/^00*//')
    if [[ ! -f ${new} ]] ;then
        mv ${old} ${new}
    else
        echo "${old} conflicts with ${new}"
    fi
done

Of course, rename is a better option if available. I'm just including this for completeness in case you're running on a UNIX box that doesn't have that tool.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜