swap-extension.sh 981 B

123456789101112131415161718192021222324
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. display_help(){
  4. echo "Usage: ${0##*/} [old-extension] [new-extension]"
  5. echo "Usage: ${0##*/} [delete-this-extension-from-filename]"
  6. exit 2
  7. }
  8. [[ $# -lt 1 ]] && display_help
  9. [[ -n $2 ]] && 2=".${2}"
  10. find . -type f -name "*.${1}" -print0 | rename --force --null "s/\.${1}\$/${2}/g" --
  11. # >>86204202 (You)
  12. # What's the point in removing the extension ? anyway, no idea if this works, so first of all try this:
  13. # find . -type f -name '*.mp3' | while IFS= read -r line; do printf 'mv -- %s %s\n' "$line" "${line%.mp3}"; done
  14. # If it seems fine to you,
  15. # find . -type f -name '*.mp3' | while IFS= read -r line; do mv -- "$line" "${line%.mp3}"; done
  16. # >>86204496 (You)
  17. # Assigning 2 ($2) does not work, unless it's zsh ... in which case, you might want to use zmv:
  18. # autoload -Uz zmv
  19. # zmv -n '**/*.mp3' '${f%.mp3}'
  20. # Remove the -n option if the commands seem fine to you. for the more readable command see >>86204501