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

后端 未结 6 545
离开以前
离开以前 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:16

    Some good answers here already; but some rely on hiding errors which is not a good idea (that assumes mv will only error because of a condition that is expected - what about all the other reaons mv might error?).

    Moreover, it can be done a little shorter and should be better quoted:

    for file in *; do
        printf -vsequenceImage 'image_%03d.jpg' "$((++i))"
        [[ -e $sequenceImage ]] || \
            mv "$file" "$sequenceImage"
    done
    

    Also note that you shouldn't capitalize your variables in bash scripts.

提交回复
热议问题