Find command issue

Guys,

Here is my requirement..

 
Sample.cfg
file="*log.gz *txt.gz"
 
sample.sh
#!/bin/sh
. $HOME/Sample.cfg
find . -name "$file" -mtime +20 -exec ls -la {} \;

Its not finding the given *log.gz and txt.gz files.

Could anyone please help me?

Thanks

I guess you can provide only one pattern to search for.

Try changing as below.

Sample.cfg
file1="*log.gz"
file2="*txt.gz"
sample.sh
#!/bin/sh
. $HOME/Sample.cfg
find . -name "$file1" -mtime +20 -exec ls -la {} \;
find . -name "$file2" -mtime +20 -exec ls -la {} \;

This will work out.. I want to know can we use one variable instead of file1 and file2.

Find will accept multiple pattern in below way

 
find . -name "*log.gz" -o -name "*txt.gz"
1 Like

Will this consider both the pattenrs?

"*log.gz" -o -name "*txt.gz"

-o is OR or AND

Thanks

Yes, there is a way for this particular scenario.

Change

file="*log.gz *txt.gz"

to

file="*[lt][ox][gt].gz"

---------- Post updated at 02:44 PM ---------- Previous update was at 02:41 PM ----------

Yes, this works for multiple matches.

That's incorrect because it matches more than just log and txt.

Regards,
Alister

Yes, I overlooked the fact that this would fetch files with patterns
*lot.gz
*lxt.gz
*tog.gz
*tot.gz
*txg.gz

Thanks for pointing it out!

You can transform that to a find expression

file="*log.gz *txt.gz"

ffile=`
set -f
out=""
for i in $file
do
 out="${out:+$out -o }-name '$i'"
done
echo "${out:+\( $out \)}"
`

eval find ... $ffile ...