Search code examples
windowsshellfindcase-sensitivecase-insensitive

Shell Script - Case-insensitive matching


This program:

#!/bin/bash

find teste1 -type f -iname "**" | while read -r firstResult
do
find teste2 -type f -iname "**" | while read -r secondResult
do
firstName=${firstResult##*[/|\\]}
secondName=${secondResult##*[/|\\]}
if [[ $firstName == $secondName ]]; then
echo "$firstResult" "$secondResult" >> equal.lst
else
echo "$firstResult" "$secondResult" >> notEqual.lst
fi
done
done

I am having a bit of a problem with it, it's working quite alright, however, when the folder is as this example: /teste1/TESTE.pub /teste2/TEstE.pub, it doesn't put the file on "equal". How can I do this? I've been trying to do the find without case sensitive, meaning it should work, but it just doesn't acknowledge.

Please help.

Maybe I should just "transform" all the files names to one of the cases and then do that search? You think that would solve my problem? In terms of logic, it would work since all files would have the same casing.


Solution

  • No need to use tr, bash has its own built in case conversion (${var,,}). Also, no need for -iname ** with find, leaving this out will match all files by default.

    #!/bin/bash
    
    find teste1 -type f | while read -r firstResult
    do
      find teste2 -type f | while read -r secondResult
      do
        firstName="${firstResult##*[/|\\]}"
        secondName="${secondResult##*[/|\\]}"
    
        if [[ "${firstName,,}" == "${secondName,,}" ]]; then
          echo "$firstResult" "$secondResult" >> equal.lst
        else
          echo "$firstResult" "$secondResult" >> notEqual.lst
        fi
      done
    done