The main problem is my directory has many files with uppercase (e.g. Foobar.txt, FooBar.txt, even FOOBAR.txt). And I find it messy to find the files by exactly typing it (if I know the exact filenames, why would I use find?). So I want to type just foobar and want all three files in result

Also, I am using * both of the side of my string to match any number of characters preceding and appending in the file name.

I want an alias or function that does this.

  • I don't see the advantage of an function here.
    – Bernhard
    Aug 27 '12 at 5:54

The correct way then would be

find -iname \*foobar\*

Where -iname is for case insensitive search, and the \ to escape the * wildcard.

The function seems a bit unnecessary for this case, but it is easy to write

function lazyfind ()
    find -iname \*$1\*
  • Hey! its hard to type the escape characters everytime, do anything.. Aug 27 '12 at 6:05
  • Well, for these simple tasks, I think it is better to type them again. Especially when working on multiple machines, it also annoying to copy functions for simple tasks.
    – Bernhard
    Aug 27 '12 at 6:15
  • @santosh, you can get the same result by using quotes: find -iname "*foo*".
    – terdon
    Aug 27 '12 at 11:10
  • 1
    And using quote is more friendly if $1 can contain spaces.
    – ams
    Aug 28 '12 at 12:03
  • And how to do that recursively? Jan 22 '13 at 8:26

Should you OS is missing GNU find -iname option, here is a portable way to achieve the same:

  while [ $i -le $l ] ; do
    lc=$(echo "$1" | cut -c $i-$i | tr '[A-Z]' '[a-z]')
    uc=$(echo "$lc" | tr '[a-z]' '[A-Z]')
    if [ ."$lc" != ."$uc" ] ; then
    i=$((i + 1))
  find . -name "*$pattern*"
  • 1
    Make that l=$(expr "x$1" : '.*') and [ $i -le $l ], in case $1 begins with -. Or embrace POSIX and write l=${#1}, the script will be faster. Or use awk, which will be easier, less error-prone and faster. Aug 27 '12 at 23:36
  • @Gilles: For some reason, I thought ${#var} wasn't posix, thanks for the head-up. I don't think there is an issue with [ $i -le $l ] , however, I also corrected $lc != $uc to .$lc != .$uc to properly handle hyphens.
    – jlliagre
    Aug 28 '12 at 0:30
  • Arithmetic expressions ($((…))) are POSIX too. I meant [ $i -lt $l ] in my first comment, because "x$1" is one character longer — but using ${#1} is clearer. Aug 28 '12 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.