get rid of all weird characters in filenames

#!/usr/bin/env bash
# rename_youtube_clean.sh

for f in *; do
  [ -f "$f" ] || continue
  case "$f" in *.sh) continue;; esac

  new="$(printf '%s' "$f" |
    tr -cd '\0-\177' |
    sed -E '
      s/\([^)]*\)//g;            # drop (...)
      s/\[[^]]*\]//g;            # drop [...]
      s/[⧸||]/_/g;              # strip slashes/pipes
      s/[–—]/-/g;                # normalise dashes
      s/  +/ /g;                 # squeeze spaces
      s/^ +//; s/ +$//;          # trim
      s/ - /-/g;                 # tighten hyphen
      s/ /_/g;                   # spaces -> underscores
      s/_+$//;                   # strip trailing underscores
      s/__+/_/g;                 # collapse duplicate underscores
    ')"

  [ -z "$new" ] && continue
  [ "$f" = "$new" ] && continue
  [ -e "$new" ] && echo "exists, skipping: $new" && continue

  mv -v -- "$f" "$new"
done

Popular posts from this blog

Automatically Fix Song Metadata and Filenames on Linux with Beets

throttle traffic on apache

script to show all files with disk usage sizes