-# Loop over the files and rename them
-for file in *; do
- # Extract the leading digits
- digits="$(echo "$file" | sed 's/\([0-9]*\).*/\1/')";
- # Zero-pad the digits
- padded_digits="$(printf "%0${max_digits}d" "$digits")";
- # Construct the new filename
- new_file="${padded_digits}${file#${digits}}";
- # Rename the file
- mv -n "$file" "$new_file"
- # declare -p file new_file; # debug
-done;
+ # Loop over the files and rename them
+ while read -r file; do
+ re='^[0-9]+';
+ if [[ ! "$file" =~ $re ]]; then continue; fi;
+
+ # Extract the leading digits
+ digits="$(echo "$file" | sed 's/\([0-9]*\).*/\1/')";
+ # Zero-pad the digits
+ padded_digits="$(printf "%0${max_digits}d" "$digits")";
+ # Construct the new filename
+ new_file="${padded_digits}${file#${digits}}";
+ # Rename the file
+ if [[ "$file" == "$new_file" ]]; then continue; fi;
+ mv -n "$file" "$new_file"
+ # declare -p file new_file; # debug
+ done < <(list_files);
+}; # main program
+
+main "$@";