Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # The purpose of this script is to find all (PDF) files in the directory
- # specified and turn the file extension to lowercase. If another file already
- # has the new name, check its MD5 hash to see if it's an identical file, and if
- # so delete it. Also delete files with any name that have identical MD5 hashes
- # to other files in the directory.
- set -eo pipefail
- usage () {
- echo -e "Usage: $(basename "$0") [dir]\n"
- exit
- }
- if [[ -z $1 || ! -d $1 ]]; then
- usage
- fi
- declare -A md5s
- regex='\.[[:alnum:]]{3}$'
- dir=$(readlink -f "$1")
- mapfile -t files < <(find "$dir" -type f -iname "*.pdf")
- for (( i = 0; i < ${#files[@]}; i++ )); do
- f="${files[${i}]}"
- f_bn=$(basename "$f")
- if [[ ! "$f" =~ $regex ]]; then
- continue
- fi
- f_no_ext="${f%.???}"
- mapfile -d'.' -t name_parts <<<"$f"
- element=$(( ${#name_parts[@]} - 1 ))
- ext="${name_parts[${element}]}"
- lc_ext=$(tr '[[:upper:]]' '[[:lower:]]' <<<"$ext")
- new_f="${f_no_ext}.${lc_ext}"
- new_f_bn=$(basename "$new_f")
- f_md5=$(md5sum -b "$f" | cut -d' ' -f1)
- if [[ -z ${md5s[${f_md5}]} ]]; then
- md5s[${f_md5}]='1'
- else
- echo "rm: ${f}"
- rm "$f"
- continue
- fi
- if [[ "$f" != "$new_f" ]]; then
- if [[ -f "$new_f" ]]; then
- new_f_md5=$(md5sum -b "$new_f" | cut -d' ' -f1)
- if [[ $f_md5 != $new_f_md5 ]]; then
- continue
- fi
- fi
- echo "mv: ${new_f}"
- mv "$f" "$new_f"
- fi
- done
RAW Paste Data