How to check file similarity case in-sensitively How to check file similarity case in-sensitively shell shell

How to check file similarity case in-sensitively


cmp uses byte-by-byte comparison, so you'll have to do your own case conversion first:

if cmp -s <(tr '[:upper:]' '[:lower:]' <"$1") <(tr '[:upper:]' '[:lower:]' <"$2"); then  echo "Mostly the same."else  echo "Different."fi
  • -s makes cmp silent - i.e., if there are differences, they are not printed.

  • <(...) is a process substitution that, loosely speaking, makes a command's output appear as a temporary, effectively self-deleting file; since process substitutions use pipes, using them is memory-efficient and works even with large output sets.

  • tr '[:upper:]' '[:lower:]' performs a locale-aware conversion of the characters provided via stdin from uppercase to lowercase, resulting in all-lowercase output.

  • Also note how $1 and $2 are double-quoted to ensure that their values are used as-is (double-quoting protects the values from shell expansions).


Perhaps:

file1=$(cat $1 | tr '[:upper:]' '[:lower:]')file2=$(cat $2 | tr '[:upper:]' '[:lower:]')if [ "$file1" = "$file2" ]; then    echo "same"else    echo "different"fi


If the files are small enough, you can do it more simply.

#!/bin/bashf1="$(<"$1")"f2="$(<"$2")"if  [[ "${f1^^}" = "${f2^^}" ]]then  echo "Mostly the same."else echo "Different."fi