Advertisement
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 () {
- printf '\n%s\n\n' "Usage: $(basename "$0") [dir]"
- exit
- }
- if [[ -z $1 || ! -d $1 ]]; then
- usage
- fi
- declare -A regex md5s
- regex[fn]='^(.*)\.([^.]*)$'
- 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[fn]} ]]; then
- continue
- fi
- no_ext="${BASH_REMATCH[1]}"
- ext="${BASH_REMATCH[2]}"
- lc_ext="${ext,,}"
- new_f="${no_ext}.${lc_ext}"
- new_f_bn=$(basename "$new_f")
- f_md5=$(md5sum -b "$f")
- f_md5="${f_md5%% *}"
- if [[ -z ${md5s[${f_md5}]} ]]; then
- md5s["${f_md5}"]=1
- else
- printf '%s\n' "rm: ${f}"
- rm "$f"
- continue
- fi
- if [[ $f != "$new_f" ]]; then
- if [[ -f $new_f ]]; then
- new_f_md5=$(md5sum -b "$new_f")
- new_f_md5="${new_f_md5%% *}"
- if [[ $f_md5 != "$new_f_md5" ]]; then
- continue
- fi
- fi
- printf '%s\n' "mv: ${new_f}"
- mv "$f" "$new_f"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement