DEV Community

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

Posted on

1

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

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay