fant0men

lc_ext.sh

Oct 18th, 2020 (edited)
593
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # The purpose of this script is to find all (PDF) files in the directory
  3. # specified and turn the file extension to lowercase. If another file already
  4. # has the new name, check its MD5 hash to see if it's an identical file, and if
  5. # so delete it. Also delete files with any name that have identical MD5 hashes
  6. # to other files in the directory.
  7.  
  8. set -eo pipefail
  9.  
  10. usage () {
  11.     echo -e "Usage: $(basename "$0") [dir]\n"
  12.     exit
  13. }
  14.  
  15. if [[ -z $1 || ! -d $1 ]]; then
  16.     usage
  17. fi
  18.  
  19. declare -A md5s
  20.  
  21. regex='\.[[:alnum:]]{3}$'
  22.  
  23. dir=$(readlink -f "$1")
  24.  
  25. mapfile -t files < <(find "$dir" -type f -iname "*.pdf")
  26.  
  27. for (( i = 0; i < ${#files[@]}; i++ )); do
  28.     f="${files[${i}]}"
  29.     f_bn=$(basename "$f")
  30.  
  31.     if [[ ! "$f" =~ $regex ]]; then
  32.         continue
  33.     fi
  34.  
  35.     f_no_ext="${f%.???}"
  36.  
  37.     mapfile -d'.' -t name_parts <<<"$f"
  38.     element=$(( ${#name_parts[@]} - 1 ))
  39.  
  40.     ext="${name_parts[${element}]}"
  41.     lc_ext=$(tr '[[:upper:]]' '[[:lower:]]' <<<"$ext")
  42.  
  43.     new_f="${f_no_ext}.${lc_ext}"
  44.     new_f_bn=$(basename "$new_f")
  45.  
  46.     f_md5=$(md5sum -b "$f" | cut -d' ' -f1)
  47.  
  48.     if [[ -z ${md5s[${f_md5}]} ]]; then
  49.         md5s[${f_md5}]='1'
  50.     else
  51.         echo "rm: ${f}"
  52.         rm "$f"
  53.  
  54.         continue
  55.     fi
  56.  
  57.     if [[ "$f" != "$new_f" ]]; then
  58.         if [[ -f "$new_f" ]]; then
  59.             new_f_md5=$(md5sum -b "$new_f" | cut -d' ' -f1)
  60.  
  61.             if [[ $f_md5 != $new_f_md5 ]]; then
  62.                 continue
  63.             fi
  64.         fi
  65.  
  66.         echo "mv: ${new_f}"
  67.         mv "$f" "$new_f"
  68.     fi
  69. done
  70.  
RAW Paste Data