Renaming a set of files to 001, 002, … on Linux

后端 未结 6 558
离开以前
离开以前 2020-12-05 07:51

I originally had a set of images of the form image_001.jpg, image_002.jpg, ...

I went through them and removed several. Now I\'d like to rename the leftover files ba

6条回答
  •  误落风尘
    2020-12-05 08:07

    If I understand right, you have e.g. image_001.jpg, image_003.jpg, image_005.jpg, and you want to rename to image_001.jpg, image_002.jpg, image_003.jpg.

    EDIT: This is modified to put the temp file in the current directory. As Stephan202 noted, this can make a significant difference if temp is on a different filesystem. To avoid hitting the temp file in the loop, it now goes through image*

    i=1; temp=$(mktemp -p .); for file in image*
    do
    mv "$file" $temp;
    mv $temp $(printf "image_%0.3d.jpg" $i)
    i=$((i + 1))
    done                                      
    

提交回复
热议问题