This command will move the contents of one or more folders to the parent folder. But if there are identical file names with the same extension then there will be files that are overwritten during the move.

find . - mindepth 2 -type f -exec mv "{}" . \; && fin d . - type d -empty -delete

How can I modify this command so that any files with duplicate file names won't be overwritten but will be appended with (1), (2), (3), et cetera?


There's an option called numbered in mv:

  numbered, t
  make numbered backups

Take a look at the mv man page

You can incorporate that into your one liner.

  • I've tried to incorporate it: find . - mindepth 2 -type f -exec mv --backup=numbered -t "{}" . \; && find . -type d -empty -delete It's not working. Any suggestions? I've tried about 6 other placements and it doesn't work. – user8547 Jul 16 '15 at 19:27
  • find . -mindepth 2 -type f | xargs mv --backup=numbered -t . && find . -type d -empty -delete – neuron Jul 16 '15 at 19:52
  • @Tx but there must be some mistake in there. It overwrites the files with the same name. – user8547 Jul 17 '15 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.