renumbering image files to be contiguous in bash renumbering image files to be contiguous in bash unix unix

renumbering image files to be contiguous in bash


Update:

Try this. If output is okay remove echo.

X=1; find . -maxdepth 1 -type f -name "*.jpg" -print0 | sort -z -n -t _ -k2 | while read -d $'\0' -r line; do echo mv "$line" "$(printf "%04d%s" $X .jpg)"; ((X++)); done


Using the super helpful rename. First, pads files with one digit to two digits; then pads files with two digits to three digits; etc.

rename IMG_ IMG_0 IMG_?.jpgrename IMG_ IMG_0 IMG_??.jpgrename IMG_ IMG_0 IMG_???.jpg

Then, your for-loop (or another similar one) that renames does the trick as the files are in both alphabetical and numerical order.


how about this :

 while read f1;do    echo $f1   mv IMG_$f1 $f1 done< <(ls | cut -d '_' -f 2 | sort -n)

thanksMichael