Commit | Line | Data |
---|---|---|
ab468b4d SBS |
1 | #!/bin/bash |
2 | # Desc: Zero-pad working dir files with initial digits for sorting | |
3 | # Usage: zeropad.sh | |
7328c729 | 4 | # Version: 0.0.2 |
ab468b4d SBS |
5 | |
6 | # Find the maximum number of leading digits in the filenames of working dir | |
7328c729 | 7 | max_digits="$(ls -1 * | sed 's/[^0-9].*//' | awk '{ if(length > L) L=length } END { print L }')"; |
ab468b4d SBS |
8 | declare -p max_digits; |
9 | ||
10 | # Loop over the files and rename them | |
11 | for file in *; do | |
12 | # Extract the leading digits | |
7328c729 | 13 | digits="$(echo "$file" | sed 's/\([0-9]*\).*/\1/')"; |
ab468b4d | 14 | # Zero-pad the digits |
7328c729 | 15 | padded_digits="$(printf "%0${max_digits}d" "$digits")"; |
ab468b4d SBS |
16 | # Construct the new filename |
17 | new_file="${padded_digits}${file#${digits}}"; | |
18 | # Rename the file | |
19 | mv -n "$file" "$new_file" | |
20 | # declare -p file new_file; # debug | |
21 | done; | |
22 | ||
7328c729 SBS |
23 | # Author: Steven Baltakatei Sandoval |
24 | # License: GPLv3+ |