Skip to content

Instantly share code, notes, and snippets.

@eailfly
Last active October 21, 2021 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eailfly/1f590b5cca2ce65fee202d9aef2dc3b2 to your computer and use it in GitHub Desktop.
Save eailfly/1f590b5cca2ce65fee202d9aef2dc3b2 to your computer and use it in GitHub Desktop.

This is from askubuntu.com

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment