Flattening a nested directory

cpdirectoryfilesrecursiverename

This is probably very simple, but I can't figure it out. I have a directory structure like this (dir2 is inside dir1):

/dir1
    /dir2 
       |
        --- file1
       |
        --- file2

What is the best way to 'flatten' this director structure in such a way to get file1 and file2 in dir1 not dir2.

Best Answer

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. The -i makes mv ask before overwriting any duplicates; you can substitute -f to overwrite them without asking (or -n to not ask or overwrite).

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 ';'
Related Question