/home/alex/dev/rename.sh (1)

From RaySoft
#!/bin/bash -
# ------------------------------------------------------------------------------
# rename.sh
# =========
#
# Scope     Native
# Copyright (C) 2024 by RaySoft, Zurich, Switzerland
# License   GNU General Public License (GPL) 2.0
#           https://www.gnu.org/licenses/gpl2.txt
#
# ------------------------------------------------------------------------------

set -o 'noglob' -o 'nounset' -o 'pipefail' # -o 'xtrace' -o 'errexit'

# ------------------------------------------------------------------------------

# Path to the global libraries
RN_DE_GLOBAL_LIB="${HOME}/dev/lib"

# Global libraries to be loaded
RN_GLOBAL_LIBS=('nf' 'os')

# ------------------------------------------------------------------------------

RN_X_BASENAME=('/usr/local/bin/gbasename')
RN_X_FIND=('/usr/local/bin/gfind')
RN_X_GETOPTS=('/usr/bin/getopts')
RN_X_MV=('/usr/local/bin/gmv')
RN_X_PRINTF=('/usr/local/bin/gprintf')
RN_X_SORT=('/usr/local/bin/gsort')

# ------------------------------------------------------------------------------

# Test if library path is available
if [[ ! -d "${RN_DE_GLOBAL_LIB}" ]]; then
  echo "Error finding directory: ${RN_DE_GLOBAL_LIB}"
  exit 1
fi

# Load libraries
for lib in "${RN_GLOBAL_LIBS[@]}"; do
  lib="${RN_DE_GLOBAL_LIB}/${lib}.sh"

  if [[ ! -f "${lib}" ]]; then
    echo "Error finding library: ${lib}!"
    exit 1
  fi

  if ! source "${lib}"; then
    exit 1
  fi
done

# Test the defined variables
if ! os::test_env 'RN'; then
  exit 1
fi

ext='jpg'
path=$(pwd)
param=('--ignore-case')

while "${RN_X_GETOPTS[@]}" ':e:p:r' option; do
  case "${option}" in
    e)
      ext="${OPTARG}"
      ;;
    p)
      path="${OPTARG}"
      ;;
    r)
      param=('--random-sort')

      if [[ -f '/dev/urandom' ]]; then
        param+=("--random-source='/dev/urandom'")
      fi
      ;;
    \?)
      nf::carp -e "Invalid option: -${OPTARG}"
      ;;
    :)
      nf::carp -e "Option -${OPTARG} requires an argument."
      ;;
  esac
done

if [[ ! -d "${path}" ]]; then
  nf::carp -e "'${path}' doesn't exist."
fi

dir="$("${RN_X_BASENAME[@]}" "${path}")"

# ------------------------------------------------------------------------------

echo 'Run 1...'

count=0

for old_file in $("${RN_X_FIND[@]}" "${path}" -maxdepth 1 -type 'f' -iname "*.${ext}")
do
  new_file="${old_file}.bak"

  if "${RN_X_MV[@]}" "${old_file}" "${new_file}" >'/dev/null' 2>&1; then
    ((count+=1))
  else
    nf::carp "'${old_file}' can't be moved"
  fi
done

echo
echo 'Run 2...'

length=${#count}
count=1

for old_file in $("${RN_X_FIND[@]}" "${path}" -maxdepth 1 -type 'f' \
                    -iname "*.${ext}.bak" \
                  | "${RN_X_SORT[@]}" "${param[@]}")
do
  new_file="$("${RN_X_PRINTF[@]}" "${path}/${dir}-%0${length}d.${ext}" ${count})"

  if "${RN_X_MV[@]}" "${old_file}" "${new_file}" >'/dev/null' 2>&1; then
    ((count+=1))
  else
    nf::carp "'${old_file}' can't be moved"
  fi
done

# ------------------------------------------------------------------------------

exit 0

Usage

NOTE:
This script uses functions from the libraries ~/dev/lib/nf.sh & ~/dev/lib/os.sh.
~/dev/rename.sh -e 'jpg' -p "${HOME}/pictures" -r