Bash Scriptlets

Bits of bash shell scripting that are handy from time to time.

#!/bin/bash

datestamp=$(date +%s)

find ./ -name '*Copy*'|grep -v '\$' |while read cn;do

 nn="$(echo "$cn"|sed 's/\(.*\) \- Copy[^\.]*\(\..*\)/\1\2/g')"

 if [ -f "$nn" ] && [ -f "$cn" ] && [ "$nn" != "$cn" ];then

  echo -e "Old: $cn\nNew: $nn"

  echo "Copy found."

  s1="$(sha256sum "$nn"|cut -f1 -d' ')"

  s2="$(sha256sum "$cn"|cut -f1 -d' ')"

  echo "Checksums: $s1 vs $s2"

  extension="${cn##*.}"

  if [ "$s1" == "$s2" ] || [ "$extension" == "lnk" ];then

   echo "Matching checksums, or link, removing extraneous copy: $cn"

   echo "echo -e \"${cn} is a duplicate of ${nn}\\n removing ${cn}\"" | tee -a /tmp/duplicates_removal.${datestamp}.bash

   echo "rm -vf \"$cn\"" | tee -a /tmp/duplicates_removal.${datestamp}.bash

  fi

 fi

done

echo "Complete the deletions with \$ bash /tmp/duplicates_removal.${datestamp}.bash