How Can I Merge Multiple Directories into One
Solution 1:
Using find
+ xargs
+ mv
:
find . -type f -print0 | xargs -0 -I file mv --backup=numbered file .
This will move all the files in the current working directory and its subdirectories (recursively) into the current working directory, numbering files with the same filename numerically in order to avoid overwrites of files with the same filename.
Sample result on a tmp
folder with a 1
, 2
and 3
subfolders each containing a 1.ext
, 2.ext
and 3.ext
file:
ubuntu@ubuntu:~/tmp$ tree
.
├── 1
│ ├── 1.ext
│ ├── 2.ext
│ └── 3.ext
├── 2
│ ├── 1.ext
│ ├── 2.ext
│ └── 3.ext
└── 3
├── 1.ext
├── 2.ext
└── 3.ext
3 directories, 9 files
ubuntu@ubuntu:~/tmp$ find . -type f -print0 | xargs -0 -I file mv --backup=numbered file .
ubuntu@ubuntu:~/tmp$ tree
.
├── 1
├── 1.ext
├── 1.ext.~1~
├── 1.ext.~2~
├── 2
├── 2.ext
├── 2.ext.~1~
├── 2.ext.~2~
├── 3
├── 3.ext
├── 3.ext.~1~
└── 3.ext.~2~
3 directories, 9 files
Solution 2:
If your directory structure looks like
dir root
- dir A
- file a
- file b
- dir B
- file c
- file d
and so on
you can do a simple
mv **/* .
to move all files at depth 1 to the root dir. Simple and Elegant!