BASH: Bulk rename

i=40; for f in *.{jpg,jpeg}; do mv "$f" " new_file_name_$(printf %02d $i).jpeg"; i=$((i+1)); done
  • i=40: Initializes a variable named i (our counter) and sets its starting value to 40.
  • for f in *.{jpg,jpeg}; do ... done: This is the loop structure.
    • for f in *.{jpg,jpeg}: It iterates through all files in the current directory that end with either .jpg or .jpeg. The current file’s name is stored in the variable $f.
    • do ... done: The actions to perform for each file.
  • mv "$f" "new_file_name_$(printf %02d $i).jpeg": This is the renaming operation.
    • mv "$f": Moves (renames) the original file ($f).
    • "new_file_NAME_... .jpeg": The new filename format.
    • $(printf %02d $i): This is a powerful part that formats the number.
      • printf %02d: Formats the integer $i to be at least two digits wide, padding with a leading zero if necessary (e.g., 40, 41, … 99, 100). If you want it to start at 40 and not be padded, you can simplify it to just $i.
  • i=$((i+1)): Increments the counter variable i by 1 for the next file.

"You're entering a world of pain."