Tuesday, 15 January 2013

shell - renumbering image files to be contiguous in bash -



shell - renumbering image files to be contiguous in bash -

i have directory image files follow naming scheme , not contiguous. e.i:

img_33.jpg img_34.jpg img_35.jpg img_223.jpg img_224.jpg img_225.jpg img_226.jpg img_446.jpg

i rename them go this, in same order:

0001.jpg 0002.jpg 0003.jpg 0004.jpg 0005.jpg 0006.jpg 0007.jpg 0008.jpg

so far came up, , while four-digit padding, doesn't sort number values in filenames.

#!/bin/bash x=1; in *; mv $i $(printf %04d.%s ${x%.*} ${i##*.}) allow x="$x+1" done

result:

img_1009.jpg 0009.jpg img_1010.jpg 0010.jpg img_101.jpg 0011.jpg img_102.jpg 0012.jpg

update:

try this. if output okay remove echo.

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

bash shell unix batch-rename

No comments:

Post a Comment