Renaming a set of files to 001, 002, ... on Linux Renaming a set of files to 001, 002, ... on Linux shell shell

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


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*domv "$file" $temp;mv $temp $(printf "image_%0.3d.jpg" $i)i=$((i + 1))done                                      


A simple loop (test with echo, execute with mv):

I=1for F in *; do  echo "$F" `printf image_%03d.jpg $I`  #mv "$F" `printf image_%03d.jpg $I` 2>/dev/null || true  I=$((I + 1))done

(I added 2>/dev/null || true to suppress warnings about identical source and target files. If this is not to your liking, go with Matthew Flaschen's answer.)


Try the following script:

numerate.sh

This code snipped should do the job:

./numerate.sh -d <your image folder> -b <start number> -L 3 -p image_ -s .jpg -o numerically -r