Search code examples
linuxbashmacosshellrename

Renaming jpg files in sequential order in Mac or Windos


I have 100 files that are named 1.jpg,2.jpg,3.jpg....100.jpg in a folder. I would like to rename them sequentially starting from number 83. Eg: 1.jpg becomes 83.jpg, 2.jpg becomes 84.jpg and so on..

I am looking for a OSX command or batch script in Windows.

Thanks.

I have tried this where i=83, but it always starts renaming 1,jpg as 100.jpg, 2.jpg as 101.jpg..so on.

i=1
for f in *; do
    [[ -f $f ]] && mv "$f" $((i++)).txt
done

Came across this script in stack forum but not sure how to set the start number to be 83 instead of default 1

ls | cat -n | while read n f; do mv "$f" "$n.txt"; done 

Solution

  • If you have 100 files that are named 1.jpg,2.jpg,3.jpg....100.jpg and you rename 1.jpg to 83.jpg then you just wiped out the original 83.jpg so you can't just rename to other numbers with the same suffix starting from 1.jpg.

    Try this, untested:

    #!/usr/bin/env bash
    
    while IFS='.' read -r num sfx; do
        mv -- "${num}.${sfx}" "$(( num + 82 )).${sfx}"
    done < <(printf '%s\n' *.jpg | sort -rn)
    

    Using sort -rn is crucial so that 83.jpg and higher get renamed before 1.jpg can overwrite 83.jpg.