Is there a Linux function to check if a filename with differing case exists?

Is there a Linux function to check if a filename with differing case exists? e.g. to find the difference between Hello.txt and hello.txt

For example if I wanted to open Hello.txt I would like to do:

  • try to open a file ( try Hello.txt)

if that fails:

  • check if there is a file with different case in the folder (find file hello.txt)

>Solution :

No, there isn’t. You’ll need to iterate the files in the directory and do the check manually. There are functions to help with that though, e.g. scandir. Since C++17 you have std::filesystem::directory_iterator to iterate through the directory and generic algorithms in the standard library to help with the implementation if needed.

Also, some file systems are case-insensitive and will consider these two equivalent file names anyway. But in general Linux considers file names as just a sequence of byte values. So you will also need to decide in what encoding you want to interpret the file name if there are non-ASCII value in there. If you assume a Unicode encoding, figuring out what exactly "differing case" means is also not trivial.

Leave a Reply