903960266b88c66b148bdedf8f1e51e09ab8dba1
2 # Desc: Zero-pad working dir files with initial digits for sorting
6 # Find the maximum number of leading digits in the filenames of working dir
7 max_digits
=$
(ls -1 * |
sed 's/[^0-9].*//' |
awk '{ if(length > L) L=length } END { print L }');
10 # Loop over the files and rename them
12 # Extract the leading digits
13 digits
=$
(echo "$file" |
sed 's/\([0-9]*\).*/\1/');
15 padded_digits
=$
(printf "%0${max_digits}d" "$digits");
16 # Construct the new filename
17 new_file
="${padded_digits}${file#${digits}}";
19 mv -n "$file" "$new_file"
20 # declare -p file new_file; # debug