2 # Desc: Zero-pad working dir files with initial digits for sorting
7 # Desc: Lists working directory file basenames
10 # Output: stdout a line-delimited list of file names
11 find .
-mindepth 1 -maxdepth 1 -type f
-exec basename '{}' \
; ;
12 }; # list pwd file basenames
14 # Find the maximum number of leading digits in the filenames of working dir
15 max_digits
="$(list_files | sed -e 's/[^0-9].*//' | awk '{ if(length > L) L=length } END { print L }')";
16 # declare -p max_digits; # debug
18 # Loop over the files and rename them
19 while read -r file; do
21 if [[ ! "$file" =~
$re ]]; then continue; fi;
23 # Extract the leading digits
24 digits
="$(echo "$file" | sed 's/\([0-9]*\).*/\1/')";
26 padded_digits
="$(printf "%0${max_digits}d
" "$digits")";
27 # Construct the new filename
28 new_file
="${padded_digits}${file#${digits}}";
30 mv -n "$file" "$new_file"
31 # declare -p file new_file; # debug
37 # Author: Steven Baltakatei Sandoval