Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to assemble a lot of images into one directory. Many of those images have the same file names.

Is there some safe version of mv that will automatically rename files if the target filename already exists so that pic1.jpeg becomes something like pic1_2.jpeg?

I could write my own python script but there has to be something like this out there so I can do:

find . -type f -name *.jpg -exec mvsafe '{}' /targetpath/ \;
share|improve this question

mv already supports this out of the box (at least in Debian):

mv --backup=t <source_file> <dest_file>

As seen in mv(1) manpage:

         make a backup of each existing destination file

The  backup  suffix  is  `~',  unless  set  with   --suffix   or   SIM‐
PLE_BACKUP_SUFFIX.   The version control method may be selected via the
--backup option or through the  VERSION_CONTROL  environment  variable.

In this case --backup=t means numbered backups.

share|improve this answer
+1 for an amazingly clean solution. BTW the option is present on RHEL 6 too. – Paweł Brodacki May 7 '11 at 12:32
While nice functionality, this does not really help that much in my case since it will append the suffix after the extension, so the files will end up with names like .jpg~1~ while I would need them to be named _1.jpg. I will probably use mmv to rename the files after moving them. – theduke May 7 '11 at 16:11
this is the mmv command that will achieve my desired result after doing mv --backup=numbered: mmv -v "..~*~" "#1_#3.#2" – theduke May 7 '11 at 16:21
On my Debian system (mmv 1.01b-18), the correct mmv command is: mmv -v "*.*~*~" "#1_#3.#2" – Xavier Jun 19 '15 at 9:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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