Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading