Operating System - HP-UX
1820270 Members
3262 Online
109622 Solutions
New Discussion юеВ

searching a file with ignore case option; find command

 
SOLVED
Go to solution
Shivkumar
Super Advisor

searching a file with ignore case option; find command

Dear Sir,

I usually use the command to search a file say BigGuy.txt, "$find / -name BigGuy.* -print".
My question is, sometime i don't know whether a file has uppercase letters in it.

If i want to use ignore case option while searching a file then what command/option do i need to use ?

Thanks,
Shiv
12 REPLIES 12
Pete Randall
Outstanding Contributor

Re: searching a file with ignore case option; find command

find / -name [Bb][Ii][Gg][Gg][Uu][Yy].* -print


Pete

Pete
baiju_3
Esteemed Contributor

Re: searching a file with ignore case option; find command

Shiv ,

for details of regular exp .

man 5 regexp .

Regards ..bl.
Good things Just Got better (Plz,not stolen from advertisement -:) )
Shivkumar
Super Advisor

Re: searching a file with ignore case option; find command

This option is cumbersome. I was looking out for some other options.
Chan 007
Honored Contributor

Re: searching a file with ignore case option; find command

Shiv,

If your .txt will remain constant,

then,

use

find / -name *.txt -print |grep -i bigguy

Cheers.

Chan
James R. Ferguson
Acclaimed Contributor
Solution

Re: searching a file with ignore case option; find command

Hi SHiv:

# cat ./finder
#!/usr/bin/perl -l
#@(#)finder finder $ Find files by name case-insensitively - JRF $

use strict;
use warnings;
use File::Find ( );
use vars qw( *name );
use Cwd;

die "Usage $0 [directory ...] file\n" unless @ARGV;

my $regex = pop;
@ARGV = cwd unless @ARGV;
*name = *File::Find::name;

sub wanted {
return unless m/^$regex\z/is;
print $name;
} # wanted

File::Find::find( \&wanted, @ARGV );

1;
#_(jrf)

Run like this (pass the directory and the file name as two arguments:

# ./finder /var SySlOg.LoG

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: searching a file with ignore case option; find command

Hi Shiv:

Here's an improved version of the above (attached). It removes the anchoring of the matched token to matching exactly the basename.

If you use regular expressions as your filename argument, surround them with sigle quotes so that the shell doesn't try to interprest them.

You can do things like:

# ./finder /tmp 'perl.+\.depot'

...which might return:

/tmp/perl_D.5.8.2.A_HP-UX_B.11.11_32+64.depot

# .finder /var SySlOg

...which might return:

/var/adm/syslog
/var/adm/syslog/syslog.log
/var/adm/syslog/OLDsyslog.log
/var/run/syslog.pid

Regards!

...JRF...
Arunvijai_4
Honored Contributor

Re: searching a file with ignore case option; find command

Hi Shiv,

If you are working on Linux, try using "locate" command. Its very easy to use and gives good result.

http://www.computerhope.com/unix/ulocate.htm

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Steve Faidley
Valued Contributor

Re: searching a file with ignore case option; find command

I have always had the same question as you but never worked it out until you asked.
Here is a short script I just wrote. It appears to solve our problem.
The case you type the filename in does not matter. The script generates the reg exp for you.
You could name this script find and put it in a dir that comes before the real find(/bin/find) in your PATH.

#!/bin/ksh
FINDME=$2
LEN=`echo ${FINDME} | wc -c`
CHAR_CNT=1
NAME=""
while true
do
CHAR=`echo ${FINDME} | cut -c ${CHAR_CNT}`
UPPER=`echo $CHAR | tr "[:lower:]" "[:upper:]"`
LOWER=`echo $CHAR | tr "[:upper:]" "[:lower:]"`
if [[ $CHAR = [A-Za-z] ]];then
NAME=${NAME}"[${UPPER}${LOWER}]"
else
NAME=${NAME}"${CHAR}"
fi
CHAR_CNT=`echo "$CHAR_CNT + 1"|bc`
if [[ $CHAR_CNT -eq LEN ]]
then
break
fi
done

echo find $1 -name "$NAME" $3 $4 $5 $6 $7 $8 $9
find $1 -name "$NAME" $3 $4 $5 $6 $7 $8 $9


OUTPUT:

Short:
./Find /tmp SEND*
find /tmp -name [Ss][Ee][Nn][Dd]*
/tmp/sendchkdb
/tmp/send.ndm
/tmp/send.WB
/tmp/send.33
/tmp/send.28

Long:
./Find /tmp SEND* -exec ll -adl {} \;
find /tmp -name [Ss][Ee][Nn][Dd]* -exec ll -adl {} ;
-rwxr-xr-x 1 root sys 229 Dec 2 2004 /tmp/sendchkdb
-rwxrwxrwx 1 root sys 474 Jan 26 13:28 /tmp/send.ndm
-rwxrwxrwx 1 root sys 933 May 20 2004 /tmp/send.WB
-rwxrwxrwx 1 root sys 933 May 20 2004 /tmp/send.33
-rwxrwxrwx 1 root sys 933 May 20 2004 /tmp/send.28
If it ain't broke, let me have a look at it.
Yogeeraj_1
Honored Contributor

Re: searching a file with ignore case option; find command

hi,

another easy way would be to do a normal find and grep the output ignoring the case.

e.g.
$ find . -print |grep -i bigguy
./test/BigGuy
$ touch test/bIggUy
$ find . -print |grep -i bigguy
./test/BigGuy
./test/bIggUy
$



this of course would be applicable to file systems with relatively limnited number of files.


hope this helps too!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Muthukumar_5
Honored Contributor

Re: searching a file with ignore case option; find command

Find is not having an option like -i with grep utility. May you can try as,

$ find / -name "*" -type f | grep -i bigguy

It will help. Else you have to try as,

$ find / -name "[Bb][iI][gG][gG][uU][Yy].*" -type f -print

Note: Try to use -type f option with find always.

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: searching a file with ignore case option; find command

Hi Shiv,

You can even try this,

# find / -type f |grep -i "bigguy"

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Alexander Skwar
Frequent Advisor

Re: searching a file with ignore case option; find command

You could also install the GNU findutils (from Porting Center http://hpux.asknet.de/hppd/hpux/Gnu/findutils-4.2.27/ or compiled from source).

GNU find has an -iname/-ipath/-iregex/-i test, which does the test in case insensitive mode.