Logo

dev-resources.site

for different kinds of informations.

My personal detox command

Published at
6/28/2024
Categories
detox
termux
tools
rename
Author
voyeg3r
Categories
4 categories in total
detox
open
termux
open
tools
open
rename
open
Author
7 person written this
voyeg3r
open
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
termux Article's
30 articles in total
Favicon
่ฎฉๅฎ‰ๅ“ๆ‰‹ๆœบไธๅ†ๅƒ็ฐ๏ผšๅœจๅฎ‰ๅ“ๆ‰‹ๆœบไธŠๆญๅปบ Rust ๅผ€ๅ‘็Žฏๅขƒ
Favicon
How to install React and Tailwind in Termux
Favicon
Small prototype... easy coding flash cards on android
Favicon
Run qBittorrent in Android
Favicon
Setup pygame in termux
Favicon
Termux-Ngrok Configuration
Favicon
How To Install Termux Box In Termux
Favicon
My personal detox command
Favicon
Como rodar um servidor NodeJS em um smartphone Android
Favicon
Run a Linux Distro in your Android device
Favicon
Install Official VS Code on Android
Favicon
Automatically Change IP Address ุชุบูŠุฑ ุงูŠ ุจูŠ ุจุดูƒู„ ุชู„ู‚ุงุฆูŠ ูƒุงู„ูŠ ู„ูŠู†ูƒุณ
Favicon
Linux On The Go (Termux)
Favicon
How To Install Lemon Rat In Termux 2024
Favicon
git diary
Favicon
Line Highlighting in Zsh Termux
Favicon
How To Install Code-Server On Android
Favicon
Installing NodeJS In Termux
Favicon
Obsidian Notes with git-crypt ๐Ÿ”
Favicon
Install Pfetch on Termux
Favicon
Youtube Two Click MP3 Downloader - Termux!
Favicon
HOW TO INSTALL NGROK IN TERMUX ANDROID
Favicon
Setting up Termux and a Linux Distro on Android
Favicon
New Github Repo
Favicon
Termux logo in python
Favicon
Termux getting terminated after 15s on starting the VNC server.
Favicon
Setting Up Termux and Node.js on Android for Web Development
Favicon
Programando pelo Tablet
Favicon
How to connect your android device to github using ssh key with termux
Favicon
Host TOR Hidden service (DARK WEB website) on a smartphone !!

Featured ones: