Linux: Case-insensitive file searching with locate and find

Earlier today someone asked for the source code for my TypeAhead predictive-text, type-ahead, auto-complete JNLP Swing application. While trying to remember where I put it I realized I was going to have to do some case-insensitive file searching.

I was happy to learn that both of my favorite Unix and Linux file-finding utilities support case-insensitive file searching. Both the find command and the locate command have command-line options that provide this support.

Back to top

Case-insensitive file searching with the locate command

It's easy to perform a case-insensitive file search with the Linux locate command: just add the -i flag. To search my entire filesystem for files and directories that contain the string typeahead, just use this command:

locate -i typeahead
Back to top

Case-insensitive file searching with the find command

If for some reason you can't find your files with the Linux locate command, or your system doesn't have the locate command installed, you can also try searching with the traditional Unix find command. Here's how I did a case-insensitive search trying to find the same typeahead files with the find command:

find . -type f -iname "*typeahead*"

(FWIW, I add the -type f option to tell find to just look for files, and not directories.)

Note that on some systems you may also have to use the -print option at the end of that command, like this:

find . -type f -iname "*typeahead*" -print

The key to that case-insensitive search is the use of the -iname option, which is only one character different from the -name option. The -iname option is what makes the search case-insensitive.

Back to top

More find command options

The Linux find command has several more case-insensitive operators, including these, which I'm taking directly from the find command man page:

-ilname pattern
    Like -lname, but the match is case insensitive.
    If the -L option or the -follow option is in effect,
    this test returns false unless the symbolic link is broken.

-iregex pattern
    Like -regex, but the match is case insensitive.

-iwholename pattern
    Like -wholename, but the match is case insensitive.
Back to top

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.