Operating System - Linux
1828581 Members
2161 Online
109982 Solutions
New Discussion

Find Command + Case Insensitive

 
james eacret
New Member

Find Command + Case Insensitive

I am attempting to write a shell script that searches for two differently named files that were created the day before the system time, then add up those file sizes.

I have:
DIRS="DIR1 DIR2 DIR3 DIR4"

for dir in $DIRS; do
echo -e "$dir: " && find . \( -name "${dir}_*" -o -name "c-*" \) -ctime -1 -exec ls -l {} \; |awk '/^-/ {total += $5} END {printf "%15.2f\n",total}'
done

Which mostly works, it just has 1 problem. I need the name to not be case sensitive. I thought -iname would do the trick, but it does not seem to be valid. I would greatly appreciate some help.

The reason it needs to be case insensitive is because 1 dir is lowercase, and the files in it are uppercase.

Ex:
dir = test
file = TEST_1122333

Thanks!
5 REPLIES 5
Rodney Hills
Honored Contributor

Re: Find Command + Case Insensitive

You need to get the gnu find from the Porting and Archive center. it has the iname option.

http://hpux.cs.utah.edu/hppd/hpux/Gnu/findutils-4.2.20/

HTH

-- Rod Hills
There be dragons...
Kofi ARTHIABAH
Honored Contributor

Re: Find Command + Case Insensitive

Hi James:
You might want to download the gnu findutils available from http://hpux.cs.utah.edu/hppd/hpux/Gnu/findutils-4.2.20/ or your nearest porting and archiving centre. it supports -iname.

Enjoy.
nothing wrong with me that a few lines of code cannot fix!
james eacret
New Member

Re: Find Command + Case Insensitive

Hello, thank you. I will talk to the server admin about installing that.

James
James R. Ferguson
Acclaimed Contributor

Re: Find Command + Case Insensitive

Hi James:

Yet another alternative is to craft your own using perl. 'stat() will yield the values for the 'ls' output. The 'File::Find' module will drive the recursive directory lookup.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Find Command + Case Insensitive

The Gnu version of find will do this quite easily but if you insist upon using standard find then you need to construct -name patterns that look like this:

-name "[Dd][Ii][Rr]1"

Here's a function that will do that:

#!/usr/bin/sh

make_regex()
{
typeset -u UC="${1}"
typeset -i LEN=$(expr length "${UC}")
typeset -i I=1
typeset OUT=''
while [[ ${I} -le ${LEN} ]]
do
typeset U1=$(echo ${UC} | cut -c ${I}-${I})
typeset L1=$(echo ${U1} | tr "[A-Z]" "[a-z]")
if [[ "${U1}" != "${L1}" ]] # is alpha
then
OUT="${OUT}[${U1}${L1}]"
else
OUT="${OUT}${U1}"
fi
((I += 1))
done
echo "${OUT}"
return 0
} # make_regex

# use it like this:

while [[ ${#} -ge 1 ]]
do
X=$(make_regex "${1}")
echo "${1} ==> ${X}"
shift
done

If it ain't broke, I can fix that.