From ab468b4d87e3850311da305c4ed50ccdd770d287 Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Mon, 15 Jul 2024 21:11:11 +0000 Subject: [PATCH] feat(user/zeropad.sh):Add script to zeropad filenames --- user/zeropad.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100755 user/zeropad.sh diff --git a/user/zeropad.sh b/user/zeropad.sh new file mode 100755 index 0000000..9039602 --- /dev/null +++ b/user/zeropad.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# Desc: Zero-pad working dir files with initial digits for sorting +# Usage: zeropad.sh +# Version: 0.0.1 + +# Find the maximum number of leading digits in the filenames of working dir +max_digits=$(ls -1 * | sed 's/[^0-9].*//' | awk '{ if(length > L) L=length } END { print L }'); +declare -p max_digits; + +# 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; + + -- 2.30.2