Loop multiple directory, find a file and send email

Hello ALL,

need a BASH script who find file and send email with attachment.
I have 50 folders without sub directories in each generated files of different sizes but with a similar name Rp01.txt Rp02.txt Rp03.txt ...etc. Each directors bound by mail group, I need a script that goes as directories and detect file sending mail attachment of the email group.

example

/home/test/test01
/home/test/test02
/home/test/test03
/home/test/test04
/home/test/........
/home/test/test50

/home/test/test01 = test01@example.com
/home/test/test02 = test02@example.com
/home/test/test03 = test03@example.com
......
/home/test/test50 = test50@example.com

script : when find file in directory test01 --> auto send mail with attachment file to test01@example.com

I hope I described things well, sorry for bad English

mutt -s "Subject" -a $(find /home/test/test01 -type f -name "Rp*.txt") -- test01@example.com < /dev/null

if use above line "mutt..." when i need a 50 rows crontab :slight_smile:

Try (untested)

cd /home/test
find ./ -iname "rp*.txt" | sort |  awk -F"/" -v S="$Subject" '
LAST && $2 != LAST      {gsub (/\.\//, "", ATT)
                         print "mutt -s \"" S "\" -a \"" ATT "\" " $2 "@example.com"
                         ATT = ""
                         DL  = ""
                        }
                        {ATT=ATT DL $3
                         DL = ","
                         LAST = $2
                        }
END                     {gsub (/\.\//, "", ATT)
                         print "mutt -s \"" S "\" -a \"" ATT "\" " $2 "@example.com"
                        }
' | sh