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

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

    This does the reverse of what you are asking (taking files of the form *.jpg.001 and converting them to *.001.jpg), but can easily be modified for your purpose:

    for file in * 
    
    do
    
    if [[ "$file" =~ "(.*)\.([[:alpha:]]+)\.([[:digit:]]{3,})$" ]]
    
    then
    
    mv "${BASH_REMATCH[0]}" "${BASH_REMATCH[1]}.${BASH_REMATCH[3]}.${BASH_REMATCH[2]}"
    
    fi
    
    done
    

提交回复
热议问题