Let's say I have a directory with many subdirectories and files. I want to copy every file with a certain extension to another directory while adding the subdirectory name to the filename. For example, a .txt file at ./directory/subdirectory/example.txt would be copied to ./destinationdir/subdirectory_example.txt. The program can be a bash script, a python script, or whatever.
I have tried things like
find ./ -name '*.txt' -exec cp -r '{}' './destination/' ';'
but it doesn't add the subdirectory name to the file, and files with the same name will just get overwritten. Any solutions?