copy some files from users home folders to my folder

i have users home directories in /home
all the users have some files starting with character e

and i want to copy all these files in a folder in my (root) home
using a script

i tried the script
for i in m5[01-48]
do
cd m5[01-48]
cp e1* /home/pc/exam
cd ..
done
but get these errors
./cpex: line 3: cd: m5[01-48]: No such file or directory
cp: cannot stat `e1*': No such file or directory

pls. help

Hi.

The shell isn't able to expand m5[01-48], so the value of i will be exactly m5[01-48].

Even if the shell could expand it, your cd statement is attempting to change into all of the directories at the same time!

cd /home
for i in m5*; do
  cd $i
  cp e1* /home/pc/exam
  cd ..
done

so you have dir named m501,m502...m548???

for i in m5* ; do
cd $i
cp e1* /home/pc/exam
cd ..
done

Hi,

Below script can give you directories from m501-m548

for i in `ls -1d m5[0-4][0-9]|grep -vE "m500|m549"`
do
cd m5[01-48]
cp e1* /home/pc/exam
cd ..
done

Regards,

Ranjith