I want to search file in 2 different directories using find command,

find $file_dir1 $file_dir2  -name 'searchpattern*' | tail -10

I am using this command now, but it is case-sensitive search. I tried -iname, it is not supported in my linux box. I need to list all files ignoring case.

find "$file_dir1" "$file_dir2" \
  -name '[sS][eE][aA][rR][cC][hH][pP][aA][tT][tT][eE][rR][nN]*'

That is we replace letters like x with the [xX] bracket expression which matches on either x or X, effectively replacing a case sensitive match of lowercase x with a case insensitive match of x.

For some characters like where the upper-case or lower-case translation isn't a single characters, you may need to resort to things like:

find "$file_dir1" "$file_dir2" \
  \( -name '*[sS][uU]ffi[xX]' -o -name '*[sS][uU]FFI[xX]' \)

as [ffiFFI] would match on , F or I instead of or FFI.

If you can guarantee your file and directory names don't contain newline characters, you could also get find to print every file path, and use awk to filter on the file name:

find "$file_dir1" "$file_dir2" |
  awk -F/ 'tolower($NF) ~ /^searchpattern/'

(beware it's not necessarily equivalent to toupper($NF) ~ /^SEARCHPATTERN/ like in the case above or because not everybody (every locale) agrees on what the upper-case or lower-case variant of a letter is (for instance, is uppercase i I or İ?); you'll also find some variations in grep -i or other tool that do case insensive matching).

  • here 'searchpattern' is just parameter example. it is not the file name itself, it should be like[a-A,z-Z]. – Shreyas RK Feb 7 '19 at 15:04
  • @ShreyasRK, whatever the pattern, you should be able to adapt using that technique. In any case, there's no saying what [a-A,z-Z] specifically would match, even in the C locale. – Stéphane Chazelas Feb 7 '19 at 15:09
  • @ Stéphane Chazelas, here we dont know wthat the search pattern will be. And will "find" search based on the first letter, or will it search from middle also. – Shreyas RK Feb 7 '19 at 16:11
  • @ShreyasRK, [sS] matches on either s or S, so -name '[sS]*' matches on files whose name starts with s or S followed by 0 or more characters (*). – Stéphane Chazelas Feb 7 '19 at 16:21

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.