From f48d0d553a0cad85352274674db42aaf4b58970f Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Mon, 15 Jul 2024 21:58:45 +0000 Subject: [PATCH 1/1] feat(user/zeropad.sh):Permit ignoring of file extension via arg --- user/zeropad.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/user/zeropad.sh b/user/zeropad.sh index e008b71..ad281b1 100755 --- a/user/zeropad.sh +++ b/user/zeropad.sh @@ -1,16 +1,30 @@ #!/bin/bash # Desc: Zero-pad working dir files with initial digits for sorting -# Usage: zeropad.sh -# Version: 0.0.5 +# Usage: zeropad.sh [str fext] +# Example: zeropad.sh; +# zeropad.sh ".jpg"; +# Version: 0.1.0 + +declare fext; # file extension list_files() { # Desc: Lists working directory file basenames # Usage: list_files - # Input: none + # Input: fext var file extension # Output: stdout a line-delimited list of file names - find . -mindepth 1 -maxdepth 1 -type f -exec basename '{}' \; ; + if [[ -z "$fext" ]]; then + find . -mindepth 1 -maxdepth 1 -type f -exec basename '{}' \; ; + else + find . -mindepth 1 -maxdepth 1 -type f -exec basename '{}' \; \ + | grep -E -- "${fext}$" ; + fi; }; # list pwd file basenames main () { + # Read file extension if provided + if [[ -n "$1" ]]; then + fext="$1"; + fi; + # Find the maximum number of leading digits in the filenames of working dir max_digits="$(list_files | sed -e 's/[^0-9].*//' | awk '{ if(length > L) L=length } END { print L }')"; # declare -p max_digits; # debug -- 2.30.2