How to flatten directory tree using 'find' on Linux
You can use the following command to recursively move all files from subdirectories to the current directory. The information of the original directory structure will be lost, however files with the same name in different directories will NOT be overwritten due to mv --backup=numbered
:
find . -type f -depth -exec mv {} --backup=numbered $(pwd) -v \;
This command does the following:
find .
- starts searching in the current directory-type f
- only finds files-depth
- processes the directory tree from the bottom up, so that files are moved before their parent directories-exec
- executes the command that follows for each file foundmv {}
- moves the file found byfind
--backup=numbered
- creates a numbered backup of files with the same name$(pwd)
- specifies the destination directory (the current directory)-v
- enables verbose output, so you can see which files are being moved\;
- indicates the end of the-exec
command
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow