Bash selective copy folders and content to another location

I'm looking for a bash scrypt to copy some folders and some of the content to another location. I'm a teacher and very noobish with programming language anyway what I'm looking for , I have this director structure

Main director "Students" with subfolders "john";"daisy";"work" etc .. and some of the folders contain a specific file for example "exam.dat" , I need a scrypt that will check every folder in students for the file "exam.dat" and copy that folder and this file to another location, without copying other files inside folders or folders that doesn't contain exam.dat.

Hope I'm explicit enough. Thank you in advance

cd /path/to/basefolder
tar -cf - */exam.dat | tar -C /path/to/destfolder -xvf -
1 Like

This approach will work fine as long as exam.dat is directly under the student folder but if you have john/class_work/exam.dat it will get missed.

This changed approach will work for this case:

cd /path/to/basefolder
tar -cf - $(find . -name exam.dat -type f) | tar -C /path/to/destfolder -xvf -

Note that if no files exist with the required name, you may get an error from tar along the lines of:

tar: Cowardly refusing to create an empty archive