How to flatten a directory on Linux and MacOS

If you want to flatten a directory with lots of deeply nested files (for example ./2012/06/09/images/previews/200x200/image1.jpg becomes ./image1.jpg), you can run this simple command:

find target/ -mindepth 2 -type f -exec mv -i '{}' target/ ';'

All the files in target‘s subdirectories will be moved directly under target. If multiple files have the same name (target/hello.txt, target/backup/hello.txt, target/hello/english/hello.txt), you will be asked to overwrite them:

overwrite ./hello.txt? (y/n [n])

The default option is “no”, so you can just hold the Enter key to say no to all overwrites.

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see