0

I would like to check the file existence using shell script. I used below code. If I have TEST.png or test.png, for both the file names if condition is succeeding and printing File found. But I would like to enable case sensitivity for file check. Please let me know how to enable case sensitivity in shell script.

    file="/mnt/floppy/test.png"
    if [ -f "$file" ]
    then
         echo "File found"

    else
         echo "Invalid file" 
    fi     
2
  • Shell/bash is actually case sensitive per default Commented Mar 25, 2016 at 11:01
  • 3
    Judging by the file path you're trying to check, you're dealing with the case insensitive filesystem, probably VFAT. Commented Mar 25, 2016 at 11:02

2 Answers 2

3

Take a look at VFAT's case sensitivity mount options:

check=s: strict, case sensitive

check=r: relaxed, case insensitive

check=n: normal, default setting, currently case insensitive


Source: https://www.kernel.org/doc/Documentation/filesystems/vfat.txt

Sign up to request clarification or add additional context in comments.

Comments

-1

You can use grep:

file="/mnt/floppy/test.png"
echo $file | grep [A-Z] && echo "Invalid file: Filename has uppercase letters" && exit 1
# echo file | grep [a-z] && echo "Valid file"

2 Comments

Also (since someone is bound to mention this) you can use the POSIX character classes [[:upper:]] and [[:lower:]] instead of [A-Z] and [a-z].
You can do this entirely in the shell; case $file in *[A-Z]*) echo Uppercase;; esac -- but the question seems to be about finding whether a file exists, regardless of case variations in the file name in the variable, which is a rather harder problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.