DEV Community

Cover image for My personal detox command
Sérgio Araújo
Sérgio Araújo

Posted on

My personal detox command

The original detox

It is perfect but in termux i do not have it in my repositories, so:

#!/bin/bash
#
# Script: dtx
# Description: Rename files by removing special characters and normalizing names.
#
# Usage: dtx [-d] [-h] files...
# Options:
#   -d    Dry run. Show the final result without renaming.
#   -h    Show this help message.
#
# Dependencies: Requires Python 3 and the 'unidecode' module. Install with:
#   pip install unidecode
#

# Get the script name dynamically
script_name=$(basename "${BASH_SOURCE[0]}")

show_help() {
  echo "Usage: $script_name [-d] [-h] files..."
  echo "Rename files by removing special characters and normalizing names."
  echo
  echo "Options:"
  echo "  -d    Dry run. Show the final result without renaming."
  echo "  -h    Show this help message."
}

check_unidecode() {
  python3 -c 'import unidecode' >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "Error: Python module 'unidecode' not found."
    echo "Please install it using:"
    echo "    pip install unidecode"
    exit 1
  fi
}

dry_run=false

# Check if unidecode module is available
check_unidecode

# Parse options
while getopts "dh" opt; do
  case ${opt} in
    d )
      dry_run=true
      ;;
    h )
      show_help
      exit 0
      ;;
    \? )
      show_help
      exit 1
      ;;
  esac
done
shift $((OPTIND -1))

if [ "$#" -lt 1 ]; then
  show_help
  exit 1
fi

for file in "$@"; do
  if [ -d "$file" ]; then
    # Transliterate UTF-8 characters to ASCII using Python unidecode for directories
    new_filename=$(echo "$file" | python3 -c 'import sys, unicodedata; filename = sys.stdin.read().strip(); filename = unicodedata.normalize("NFD", filename).encode("ascii", "ignore").decode("ascii").replace(" ", "-").replace("_", "-").replace("/", "-").replace(".", "-"); filename = "-".join(filter(None, filename.split("-"))); print(filename)')

    # Check if the directory name needs to be changed
    if [ "$file" != "$new_filename" ]; then
      if [ "$dry_run" = true ]; then
        echo "Would rename directory: '$file' -> '$new_filename'"
      else
        mv -v "$file" "$new_filename"
      fi
    else
      if [ "$dry_run" = true ]; then
        echo "Skipping directory: '$file', already correctly named."
      fi
    fi
  else
    # Separate filename and extension using parameter expansion
    filename=$(basename -- "$file")
    extension="${filename##*.}"
    filename="${filename%.*}"

    # Transliterate UTF-8 characters to ASCII using Python unidecode for files
    new_filename=$(echo "$filename" | python3 -c 'import sys, unicodedata; filename = sys.stdin.read().strip(); filename = unicodedata.normalize("NFD", filename).encode("ascii", "ignore").decode("ascii").replace(" ", "-").replace("_", "-").replace("/", "-").replace(".", "-"); filename = "-".join(filter(None, filename.split("-"))); print(filename)')

    # Construct the new filename with the preserved extension
    newname="${new_filename}.${extension}"

    # Check if the filename needs to be changed
    if [ "$file" != "$newname" ]; then
      if [ "$dry_run" = true ]; then
        echo "Would rename file: '$file' -> '$newname'"
      else
        mv -v "$file" "$newname"
      fi
    else
      if [ "$dry_run" = true ]; then
        echo "Skipping file: '$file', already correctly named."
      fi
    fi
  fi
done
Enter fullscreen mode Exit fullscreen mode

Top comments (0)