Using prename
(aliased to rename
on many systems) it is possible to do this without multiple runs of mv
or cp
:
cd /home/user
find . -type f -name 'Loads.bdf' -exec \
prename -n 's:^./::; s:/:_:g; s:^:destination/:' {} +
Remove the -n
when you are sure this is moving the files you want.
Update
To copy the files instead of moving them, the fastest way is to copy first with cpio
or pax
, then to rename as before. Eg (with GNU cpio
):
cd /home/user
find . -type f -name 'Loads.bdf | cpio -p --make-directories destination
cd destination
find . -type f -name 'Loads.bdf' -exec prename -n 's:^./::; s:/:_:g' {} +
Note that while pax
is both POSIX and LSB and cpio
is no longer POSIX, cpio
is still the program which is commonly found many systems.
To handle filenames with newlines with GNU find
/cpio
it is possible to add -print0
to find
and -0
to cpio
.
Update 2
The pax
solution is a one-liner (not including the cd
):
cd /home/user
find . -name 'Loads.bdf' | cut -d/ -f 2- | pax -rws ':/:_:g' destination
Or for any filename (and GNU find
):
find /home/user -name 'Loads.bdf' -printf '%P\0' | pax -0rws ':/:_:g' destination