UNIX is case-sensitive. So, if you are searching for a file, but not sure of it’s case – whether it’s uppercase, lowercase or a mix of both, fret not, it’s not that difficult.
The solution to perform a case insensitive search is to use the find
command. We need to pass a few parameters to the find
command though.
directory – starting point in the directory hierarchy for the search
filename – the name of the file we are searching for
If we want to search for a file called “foo.c
” in the HOME directory (in my case, HOME directory is /home/ibrahim):
1 |
$ find /home/ibrahim -name foo.c -print |
The above command will search all the files in my HOME directory that are named exactly as foo.c
. It will not match files called “FOO.C
” or “Foo.c
“.
1 |
/home/ibrahim/tmp/foo.c |
To perform a case insensitive search, we will need to change the -name
parameter to -iname
instead.
1 |
$ find /home/ibrahim -iname foo.c -print |
Below is a sample output of the find
command.
1 2 3 |
/home/ibrahim/tmp/foo.c /home/ibrahim/tmp/Foo.c /home/ibrahim/tmp/FOO.C |
To search only for files and exclude directory matches, pass “-type f
” parameter to the find
command.
1 |
$ find /home/ibrahim -type f -iname foo.c -print |
Some variants of the find
command do not support the “-iname
” option. In that case, we will need to use the grep command to perform the case insensitive search.
1 |
$ find /home/ibrahim -type f -name "*" -print | grep -i foo.c |
The find
command will search for all files and pipe the output to the grep
command. The grep
command will then filter and print all case insensitive matches to “foo.c
” due to the “-i
” option.