Tips

Find A File While Ignoring Case Sensitivity

Today we’ll have a fairly short article as we’re simply discussing a way to find a file while ignoring case sensitivity. This can be a pretty handy command to have in your toolbox, so I’ll do what I can to turn this into an article. If you’re into finding files, especially with the terminal, you might just want to read this article!

I might even give you a bonus method! Maybe… It depends on how well you behave yourself! Don’t make me pull this car over!

Alright… Back on topic…

So, what is case sensitivity? We usually have lowercase files and commands. Linux is case-sensitive. If you type sudo APT update nothing is going to happen. Why? Because apt is lowercase. Similarly, a file named FOO.bar is not the same as foo.bar.

If you try to perform an operation on a file with the wrong case, the operation will not take place. The file isn’t found, because you’ve used the wrong case. In the above command, not even SUDO apt update will work. You have to use lowercase.

Much of Linux is in lowercase. You can’t even use capital letters in your username, by default. Sure, you can force Linux to accept a bad username, but that’s not what the system is expecting.

So too are filenames. Well, mostly… Sometimes you will come across files that use mixed case or maybe all capital letters. You might even have created a file yourself with mixed case letters. When performing an operation on those files, you need to use the correct case.

Now, you can find files while ignoring the case. That’s what this article is all about accomplishing. It shouldn’t be too long.

The Find Command:

The find command is used in the terminal, so we will have to open the terminal to follow along with today’s article. However, on the plus side, you absolutely shouldn’t need to install anything. This find command should be available by default – unless you’re on an extremely stripped-down version of Linux.

If you check the man page, you’ll see that find is the right application for this.

find – search for files in a directory hierarchy

As we’re trying to find files (while ignoring case sensitivity) that seems like one of the good tools for the job – and it is.

Find A File While Ignoring Case Sensitivity:

As mentioned above, we do this in the terminal. More often than not, you can just press CTRL + ALT + T and your default terminal should open. Otherwise, you can open your terminal via your application menu.

With your terminal now open, let’s change directories and create a file.

cd ~/Documents
touch HeLLo.txt

You can confirm that the file exists by running the ls command.

Next, try this command:

find hello.txt

Unless you have another file with that name, you will not get any results from that command. That’s why you need the -iname flag. Simply add that to your command and you can find the file while ignoring case sensitivity.

find -iname <file_name>

So, in our case, it’d be:

find -iname hello.txt

Sure enough, that finds HeLLo.txt.

If you don’t feel like changing the directory, you can change the command up a bit. The syntax would look like this:

find /path/to/directory -iname <file_name>

So, in our example:

find ~/Documents/ -iname hello.txt

And, sure enough, that finds the file while ignoring case sensitivity! 

Pretty neat and pretty easy, right?

How about that bonus?

Find A File While Ignoring Case Sensitivity (with locate):

You probably don’t have locate installed by default. You’ll need to install it.

To that end, read this previous article:

And Still Another Way To Find Files By Extension

Specifically this part is of interest (saving you a click):

With your terminal open, we can get mlocate installed with one of the following commands:

Debian/Ubuntu:

sudo apt install mlocate

Arch/Manjaro/etc:

sudo pacman -S mlocate

RHEL/CentOS/etc:

sudo yum install mlocate

OpenSUSE/GeckoLinux/etc:

sudo zypper install mlocate

There are other package managers. If your package manager isn’t covered, just go ahead and search for “mlocate” and you’ll likely find that it’s available by default.

So, with locate now installed, the syntax is also very simple. You simply need the -i flag to find files while ignoring case sensitivity.

First, we should update the locate database:

sudo updatedb

Next, try this command:

locate hello.txt

That will not find the file in your ~/Documents directory. Add the flag and run the command again, like so:

locate -i hello.txt

If you want to run that locate command in a specific directory, you can do that. The syntax is a little different than many other commands, but it’s easy enough.

locate -i <file_name> /path/to/directory

Or, in our case, it’d look like this:

locate -i hello.txt ~/Documents/

Sure enough, that outputs the information exactly as planned. It will happily ignore the case sensitivity and find your file named HeLLo.txt.

And now you’ve learned two ways to find a file while ignoring case sensitivity. One of the ways is even a nice bonus way to do accomplish this goal.

Closure:

As the headline and opening paragraph suggested, you can find a file while ignoring case sensitivity. It’s not particularly taxing and there are multiple ways to accomplish this goal. I figured I’d give some ‘bonus’ information with this one and share how to find a file while ignoring case sensitivity with the locate command used in the previous article. Why not?

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Recent Posts

Monitor System Resources With “Resources”

Today is one of those days when I'd like to introduce you to new software…

1 day ago

Short: The Halt Command

Today's article is intentionally short because we're simply going to learn about the basics of…

3 days ago

Installing Flatpaks In Linux

Today we're going to have a pretty easy article where you'll learn about installing Flatpaks…

5 days ago

A Quick Look At The Shutdown Command

This is going to be just a quick article about a command many of you…

1 week ago

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

1 week ago

View Detailed Hardware Information

There are many tools for showing your hardware information and today we'll get to view…

2 weeks ago