You can do this with GNU find
and GNU mv
:
find /dir1 -mindepth 2 -type f -exec mv -t /dir1 -i '{}' +
Basically, the way that works if that find
goes through the entire directory tree and for each file (-type f
) that is not in the top-level directory (-mindepth 2
), it runs a mv
to move it to the directory you want (-exec mv … +
). The -t
argument to mv
lets you specify the destination directory first, which is needed because the +
form of -exec
puts all the source locations at the end of the command.
As Stephane Chazelas points out, the above only works with GNU tools (which are standard on Linux, but not most other systems). The following is somewhat slower (because it invokes mv
multiple times) but much more universal:
find /dir1 -mindepth 2 -type f -exec mv -i '{}' /dir1 ';'