recursively add directory name to file name

Using Perl rename and find:

$ find source -type f | rename -n 's:(^|.*/)([^/]*)/([^/]*)$:destination/$2$3:'
rename(source/dir2/file3.ext3, destination/dir2file3.ext3)
rename(source/dir2/dir3/file4.ext4, destination/dir3file4.ext4)
rename(source/dir1/file1.ext1, destination/dir1file1.ext1)
rename(source/dir1/file2.ext2, destination/dir1file2.ext2)

The regex (^|.*/)([^/]*)/([^/]*) saves the last two components of the path (the filename and parent directory) as the second and third matched groups.

The destination directory must exist before running this. The -n is for testing, remove it for actually moving the files.