57b56c65b4a8dedaf87309f8a6f52083932f43cd
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
11 while read -r file; do
13 if [[ ! "$file" =~
$re ]]; then continue; fi;
15 # Extract the leading digits
16 digits
="$(echo "$file" | sed 's/\([0-9]*\).*/\1/')";
18 padded_digits
="$(printf "%0${max_digits}d
" "$digits")";
19 # Construct the new filename
20 new_file
="${padded_digits}${file#${digits}}";
22 mv -n "$file" "$new_file"
23 # declare -p file new_file; # debug
24 done < <(find .
-mindepth 1 -maxdepth 1 -type f
);
26 # Author: Steven Baltakatei Sandoval